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.

583 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. static uint32 lastUniquePeerID = 1;
  22. //==============================================================================
  23. ComponentPeer::ComponentPeer (Component& comp, int flags)
  24. : component (comp),
  25. styleFlags (flags),
  26. uniqueID (lastUniquePeerID += 2) // increment by 2 so that this can never hit 0
  27. {
  28. Desktop::getInstance().peers.add (this);
  29. }
  30. ComponentPeer::~ComponentPeer()
  31. {
  32. auto& desktop = Desktop::getInstance();
  33. desktop.peers.removeFirstMatchingValue (this);
  34. desktop.triggerFocusCallback();
  35. }
  36. //==============================================================================
  37. int ComponentPeer::getNumPeers() noexcept
  38. {
  39. return Desktop::getInstance().peers.size();
  40. }
  41. ComponentPeer* ComponentPeer::getPeer (const int index) noexcept
  42. {
  43. return Desktop::getInstance().peers [index];
  44. }
  45. ComponentPeer* ComponentPeer::getPeerFor (const Component* const component) noexcept
  46. {
  47. for (auto* peer : Desktop::getInstance().peers)
  48. if (&(peer->getComponent()) == component)
  49. return peer;
  50. return nullptr;
  51. }
  52. bool ComponentPeer::isValidPeer (const ComponentPeer* const peer) noexcept
  53. {
  54. return Desktop::getInstance().peers.contains (const_cast<ComponentPeer*> (peer));
  55. }
  56. void ComponentPeer::updateBounds()
  57. {
  58. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, component.getBoundsInParent()), false);
  59. }
  60. bool ComponentPeer::isKioskMode() const
  61. {
  62. return Desktop::getInstance().getKioskModeComponent() == &component;
  63. }
  64. //==============================================================================
  65. void ComponentPeer::handleMouseEvent (MouseInputSource::InputSourceType type, Point<float> pos, ModifierKeys newMods,
  66. float newPressure, float newOrientation, int64 time, PenDetails pen, int touchIndex)
  67. {
  68. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  69. MouseInputSource (*mouse).handleEvent (*this, pos, time, newMods, newPressure, newOrientation, pen);
  70. }
  71. void ComponentPeer::handleMouseWheel (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, const MouseWheelDetails& wheel, int touchIndex)
  72. {
  73. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  74. MouseInputSource (*mouse).handleWheel (*this, pos, time, wheel);
  75. }
  76. void ComponentPeer::handleMagnifyGesture (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, float scaleFactor, int touchIndex)
  77. {
  78. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  79. MouseInputSource (*mouse).handleMagnifyGesture (*this, pos, time, scaleFactor);
  80. }
  81. //==============================================================================
  82. void ComponentPeer::handlePaint (LowLevelGraphicsContext& contextToPaintTo)
  83. {
  84. ModifierKeys::updateCurrentModifiers();
  85. Graphics g (contextToPaintTo);
  86. if (component.isTransformed())
  87. g.addTransform (component.getTransform());
  88. auto peerBounds = getBounds();
  89. if (peerBounds.getWidth() != component.getWidth() || peerBounds.getHeight() != component.getHeight())
  90. // Tweak the scaling so that the component's integer size exactly aligns with the peer's scaled size
  91. g.addTransform (AffineTransform::scale (peerBounds.getWidth() / (float) component.getWidth(),
  92. peerBounds.getHeight() / (float) component.getHeight()));
  93. #if JUCE_ENABLE_REPAINT_DEBUGGING
  94. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  95. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  96. #endif
  97. {
  98. g.saveState();
  99. }
  100. #endif
  101. JUCE_TRY
  102. {
  103. component.paintEntireComponent (g, true);
  104. }
  105. JUCE_CATCH_EXCEPTION
  106. #if JUCE_ENABLE_REPAINT_DEBUGGING
  107. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  108. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  109. #endif
  110. {
  111. // enabling this code will fill all areas that get repainted with a colour overlay, to show
  112. // clearly when things are being repainted.
  113. g.restoreState();
  114. static Random rng;
  115. g.fillAll (Colour ((uint8) rng.nextInt (255),
  116. (uint8) rng.nextInt (255),
  117. (uint8) rng.nextInt (255),
  118. (uint8) 0x50));
  119. }
  120. #endif
  121. /** If this fails, it's probably be because your CPU floating-point precision mode has
  122. been set to low.. This setting is sometimes changed by things like Direct3D, and can
  123. mess up a lot of the calculations that the library needs to do.
  124. */
  125. jassert (roundToInt (10.1f) == 10);
  126. }
  127. Component* ComponentPeer::getTargetForKeyPress()
  128. {
  129. auto* c = Component::getCurrentlyFocusedComponent();
  130. if (c == nullptr)
  131. c = &component;
  132. if (c->isCurrentlyBlockedByAnotherModalComponent())
  133. if (auto* currentModalComp = Component::getCurrentlyModalComponent())
  134. c = currentModalComp;
  135. return c;
  136. }
  137. bool ComponentPeer::handleKeyPress (const int keyCode, const juce_wchar textCharacter)
  138. {
  139. ModifierKeys::updateCurrentModifiers();
  140. return handleKeyPress (KeyPress (keyCode,
  141. ModifierKeys::getCurrentModifiers().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. ModifierKeys::updateCurrentModifiers();
  181. bool keyWasUsed = false;
  182. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  183. {
  184. const WeakReference<Component> deletionChecker (target);
  185. keyWasUsed = target->keyStateChanged (isKeyDown);
  186. if (keyWasUsed || deletionChecker == nullptr)
  187. break;
  188. if (auto* keyListeners = target->keyListeners.get())
  189. {
  190. for (int i = keyListeners->size(); --i >= 0;)
  191. {
  192. keyWasUsed = keyListeners->getUnchecked(i)->keyStateChanged (isKeyDown, target);
  193. if (keyWasUsed || deletionChecker == nullptr)
  194. return keyWasUsed;
  195. i = jmin (i, keyListeners->size());
  196. }
  197. }
  198. }
  199. return keyWasUsed;
  200. }
  201. void ComponentPeer::handleModifierKeysChange()
  202. {
  203. ModifierKeys::updateCurrentModifiers();
  204. auto* target = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  205. if (target == nullptr)
  206. target = Component::getCurrentlyFocusedComponent();
  207. if (target == nullptr)
  208. target = &component;
  209. target->internalModifierKeysChanged();
  210. }
  211. TextInputTarget* ComponentPeer::findCurrentTextInputTarget()
  212. {
  213. auto* c = Component::getCurrentlyFocusedComponent();
  214. if (c == &component || component.isParentOf (c))
  215. if (auto* ti = dynamic_cast<TextInputTarget*> (c))
  216. if (ti->isTextInputActive())
  217. return ti;
  218. return nullptr;
  219. }
  220. void ComponentPeer::dismissPendingTextInput() {}
  221. //==============================================================================
  222. void ComponentPeer::handleBroughtToFront()
  223. {
  224. ModifierKeys::updateCurrentModifiers();
  225. component.internalBroughtToFront();
  226. }
  227. void ComponentPeer::setConstrainer (ComponentBoundsConstrainer* const newConstrainer) noexcept
  228. {
  229. constrainer = newConstrainer;
  230. }
  231. void ComponentPeer::handleMovedOrResized()
  232. {
  233. ModifierKeys::updateCurrentModifiers();
  234. const bool nowMinimised = isMinimised();
  235. if (component.flags.hasHeavyweightPeerFlag && ! nowMinimised)
  236. {
  237. const WeakReference<Component> deletionChecker (&component);
  238. auto newBounds = Component::ComponentHelpers::rawPeerPositionToLocal (component, getBounds());
  239. auto oldBounds = component.getBounds();
  240. const bool wasMoved = (oldBounds.getPosition() != newBounds.getPosition());
  241. const bool wasResized = (oldBounds.getWidth() != newBounds.getWidth() || oldBounds.getHeight() != newBounds.getHeight());
  242. if (wasMoved || wasResized)
  243. {
  244. component.boundsRelativeToParent = newBounds;
  245. if (wasResized)
  246. component.repaint();
  247. component.sendMovedResizedMessages (wasMoved, wasResized);
  248. if (deletionChecker == nullptr)
  249. return;
  250. }
  251. }
  252. if (isWindowMinimised != nowMinimised)
  253. {
  254. isWindowMinimised = nowMinimised;
  255. component.minimisationStateChanged (nowMinimised);
  256. component.sendVisibilityChangeMessage();
  257. }
  258. if (! isFullScreen())
  259. lastNonFullscreenBounds = component.getBounds();
  260. }
  261. void ComponentPeer::handleFocusGain()
  262. {
  263. ModifierKeys::updateCurrentModifiers();
  264. if (component.isParentOf (lastFocusedComponent)
  265. && lastFocusedComponent->isShowing()
  266. && lastFocusedComponent->getWantsKeyboardFocus())
  267. {
  268. Component::currentlyFocusedComponent = lastFocusedComponent;
  269. Desktop::getInstance().triggerFocusCallback();
  270. lastFocusedComponent->internalFocusGain (Component::focusChangedDirectly);
  271. }
  272. else
  273. {
  274. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  275. component.grabKeyboardFocus();
  276. else
  277. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  278. }
  279. }
  280. void ComponentPeer::handleFocusLoss()
  281. {
  282. ModifierKeys::updateCurrentModifiers();
  283. if (component.hasKeyboardFocus (true))
  284. {
  285. lastFocusedComponent = Component::currentlyFocusedComponent;
  286. if (lastFocusedComponent != nullptr)
  287. {
  288. Component::currentlyFocusedComponent = nullptr;
  289. Desktop::getInstance().triggerFocusCallback();
  290. lastFocusedComponent->internalFocusLoss (Component::focusChangedByMouseClick);
  291. }
  292. }
  293. }
  294. Component* ComponentPeer::getLastFocusedSubcomponent() const noexcept
  295. {
  296. return (component.isParentOf (lastFocusedComponent) && lastFocusedComponent->isShowing())
  297. ? static_cast<Component*> (lastFocusedComponent)
  298. : &component;
  299. }
  300. void ComponentPeer::handleScreenSizeChange()
  301. {
  302. ModifierKeys::updateCurrentModifiers();
  303. component.parentSizeChanged();
  304. handleMovedOrResized();
  305. }
  306. void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept
  307. {
  308. lastNonFullscreenBounds = newBounds;
  309. }
  310. const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const noexcept
  311. {
  312. return lastNonFullscreenBounds;
  313. }
  314. Point<int> ComponentPeer::localToGlobal (Point<int> p) { return localToGlobal (p.toFloat()).roundToInt(); }
  315. Point<int> ComponentPeer::globalToLocal (Point<int> p) { return globalToLocal (p.toFloat()).roundToInt(); }
  316. Rectangle<int> ComponentPeer::localToGlobal (const Rectangle<int>& relativePosition)
  317. {
  318. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  319. }
  320. Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPosition)
  321. {
  322. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  323. }
  324. Rectangle<int> ComponentPeer::getAreaCoveredBy (Component& subComponent) const
  325. {
  326. return ScalingHelpers::scaledScreenPosToUnscaled
  327. (component, component.getLocalArea (&subComponent, subComponent.getLocalBounds()));
  328. }
  329. //==============================================================================
  330. namespace DragHelpers
  331. {
  332. static bool isFileDrag (const ComponentPeer::DragInfo& info)
  333. {
  334. return ! info.files.isEmpty();
  335. }
  336. static bool isSuitableTarget (const ComponentPeer::DragInfo& info, Component* target)
  337. {
  338. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target) != nullptr
  339. : dynamic_cast<TextDragAndDropTarget*> (target) != nullptr;
  340. }
  341. static bool isInterested (const ComponentPeer::DragInfo& info, Component* target)
  342. {
  343. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target)->isInterestedInFileDrag (info.files)
  344. : dynamic_cast<TextDragAndDropTarget*> (target)->isInterestedInTextDrag (info.text);
  345. }
  346. static Component* findDragAndDropTarget (Component* c, const ComponentPeer::DragInfo& info, Component* lastOne)
  347. {
  348. for (; c != nullptr; c = c->getParentComponent())
  349. if (isSuitableTarget (info, c) && (c == lastOne || isInterested (info, c)))
  350. return c;
  351. return nullptr;
  352. }
  353. }
  354. bool ComponentPeer::handleDragMove (const ComponentPeer::DragInfo& info)
  355. {
  356. ModifierKeys::updateCurrentModifiers();
  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. ModifierKeys::updateCurrentModifiers();
  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. } // namespace juce