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.

527 lines
18KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. bool juce_performDragDropFiles (const StringArray&, const bool copyFiles, bool& shouldStop);
  18. bool juce_performDragDropText (const String&, bool& shouldStop);
  19. //==============================================================================
  20. class DragAndDropContainer::DragImageComponent : public Component,
  21. private Timer
  22. {
  23. public:
  24. DragImageComponent (const Image& im,
  25. const var& desc,
  26. Component* const sourceComponent,
  27. Component* const mouseSource,
  28. DragAndDropContainer& ddc,
  29. Point<int> offset)
  30. : sourceDetails (desc, sourceComponent, Point<int>()),
  31. image (im), owner (ddc),
  32. mouseDragSource (mouseSource),
  33. imageOffset (offset),
  34. hasCheckedForExternalDrag (false)
  35. {
  36. updateSize();
  37. if (mouseDragSource == nullptr)
  38. mouseDragSource = sourceComponent;
  39. mouseDragSource->addMouseListener (this, false);
  40. startTimer (200);
  41. setInterceptsMouseClicks (false, false);
  42. setAlwaysOnTop (true);
  43. }
  44. ~DragImageComponent()
  45. {
  46. if (owner.dragImageComponent == this)
  47. owner.dragImageComponent.release();
  48. if (mouseDragSource != nullptr)
  49. {
  50. mouseDragSource->removeMouseListener (this);
  51. if (DragAndDropTarget* const current = getCurrentlyOver())
  52. if (current->isInterestedInDragSource (sourceDetails))
  53. current->itemDragExit (sourceDetails);
  54. }
  55. owner.dragOperationEnded();
  56. }
  57. void paint (Graphics& g) override
  58. {
  59. if (isOpaque())
  60. g.fillAll (Colours::white);
  61. g.setOpacity (1.0f);
  62. g.drawImageAt (image, 0, 0);
  63. }
  64. void mouseUp (const MouseEvent& e) override
  65. {
  66. if (e.originalComponent != this)
  67. {
  68. if (mouseDragSource != nullptr)
  69. mouseDragSource->removeMouseListener (this);
  70. // (note: use a local copy of this in case the callback runs
  71. // a modal loop and deletes this object before the method completes)
  72. DragAndDropTarget::SourceDetails details (sourceDetails);
  73. DragAndDropTarget* finalTarget = nullptr;
  74. const bool wasVisible = isVisible();
  75. setVisible (false);
  76. Component* unused;
  77. finalTarget = findTarget (e.getScreenPosition(), details.localPosition, unused);
  78. if (wasVisible) // fade the component and remove it - it'll be deleted later by the timer callback
  79. dismissWithAnimation (finalTarget == nullptr);
  80. if (Component* parent = getParentComponent())
  81. parent->removeChildComponent (this);
  82. if (finalTarget != nullptr)
  83. {
  84. currentlyOverComp = nullptr;
  85. finalTarget->itemDropped (details);
  86. }
  87. // careful - this object could now be deleted..
  88. }
  89. }
  90. void mouseDrag (const MouseEvent& e) override
  91. {
  92. if (e.originalComponent != this)
  93. updateLocation (true, e.getScreenPosition());
  94. }
  95. void updateLocation (const bool canDoExternalDrag, Point<int> screenPos)
  96. {
  97. DragAndDropTarget::SourceDetails details (sourceDetails);
  98. setNewScreenPos (screenPos);
  99. Component* newTargetComp;
  100. DragAndDropTarget* const newTarget = findTarget (screenPos, details.localPosition, newTargetComp);
  101. setVisible (newTarget == nullptr || newTarget->shouldDrawDragImageWhenOver());
  102. if (newTargetComp != currentlyOverComp)
  103. {
  104. if (DragAndDropTarget* const lastTarget = getCurrentlyOver())
  105. if (details.sourceComponent != nullptr && lastTarget->isInterestedInDragSource (details))
  106. lastTarget->itemDragExit (details);
  107. currentlyOverComp = newTargetComp;
  108. if (newTarget != nullptr
  109. && newTarget->isInterestedInDragSource (details))
  110. newTarget->itemDragEnter (details);
  111. }
  112. sendDragMove (details);
  113. if (canDoExternalDrag)
  114. {
  115. const Time now (Time::getCurrentTime());
  116. if (getCurrentlyOver() != nullptr)
  117. lastTimeOverTarget = now;
  118. else if (now > lastTimeOverTarget + RelativeTime::milliseconds (700))
  119. checkForExternalDrag (details, screenPos);
  120. }
  121. forceMouseCursorUpdate();
  122. }
  123. void updateImage (const Image& newImage)
  124. {
  125. image = newImage;
  126. updateSize();
  127. repaint();
  128. }
  129. void timerCallback() override
  130. {
  131. forceMouseCursorUpdate();
  132. if (sourceDetails.sourceComponent == nullptr)
  133. {
  134. deleteSelf();
  135. }
  136. else if (! isMouseButtonDownAnywhere())
  137. {
  138. if (mouseDragSource != nullptr)
  139. mouseDragSource->removeMouseListener (this);
  140. deleteSelf();
  141. }
  142. }
  143. bool keyPressed (const KeyPress& key) override
  144. {
  145. if (key == KeyPress::escapeKey)
  146. {
  147. dismissWithAnimation (true);
  148. deleteSelf();
  149. return true;
  150. }
  151. return false;
  152. }
  153. bool canModalEventBeSentToComponent (const Component* targetComponent) override
  154. {
  155. return targetComponent == mouseDragSource;
  156. }
  157. // (overridden to avoid beeps when dragging)
  158. void inputAttemptWhenModal() override {}
  159. DragAndDropTarget::SourceDetails sourceDetails;
  160. private:
  161. Image image;
  162. DragAndDropContainer& owner;
  163. WeakReference<Component> mouseDragSource, currentlyOverComp;
  164. const Point<int> imageOffset;
  165. bool hasCheckedForExternalDrag;
  166. Time lastTimeOverTarget;
  167. void updateSize()
  168. {
  169. setSize (image.getWidth(), image.getHeight());
  170. }
  171. void forceMouseCursorUpdate()
  172. {
  173. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  174. }
  175. DragAndDropTarget* getCurrentlyOver() const noexcept
  176. {
  177. return dynamic_cast<DragAndDropTarget*> (currentlyOverComp.get());
  178. }
  179. static Component* findDesktopComponentBelow (Point<int> screenPos)
  180. {
  181. Desktop& desktop = Desktop::getInstance();
  182. for (int i = desktop.getNumComponents(); --i >= 0;)
  183. {
  184. Component* c = desktop.getComponent(i);
  185. if (Component* hit = c->getComponentAt (c->getLocalPoint (nullptr, screenPos)))
  186. return hit;
  187. }
  188. return nullptr;
  189. }
  190. DragAndDropTarget* findTarget (Point<int> screenPos, Point<int>& relativePos,
  191. Component*& resultComponent) const
  192. {
  193. Component* hit = getParentComponent();
  194. if (hit == nullptr)
  195. hit = findDesktopComponentBelow (screenPos);
  196. else
  197. hit = hit->getComponentAt (hit->getLocalPoint (nullptr, screenPos));
  198. // (note: use a local copy of this in case the callback runs
  199. // a modal loop and deletes this object before the method completes)
  200. const DragAndDropTarget::SourceDetails details (sourceDetails);
  201. while (hit != nullptr)
  202. {
  203. if (DragAndDropTarget* const ddt = dynamic_cast<DragAndDropTarget*> (hit))
  204. {
  205. if (ddt->isInterestedInDragSource (details))
  206. {
  207. relativePos = hit->getLocalPoint (nullptr, screenPos);
  208. resultComponent = hit;
  209. return ddt;
  210. }
  211. }
  212. hit = hit->getParentComponent();
  213. }
  214. resultComponent = nullptr;
  215. return nullptr;
  216. }
  217. void setNewScreenPos (Point<int> screenPos)
  218. {
  219. Point<int> newPos (screenPos - imageOffset);
  220. if (Component* p = getParentComponent())
  221. newPos = p->getLocalPoint (nullptr, newPos);
  222. setTopLeftPosition (newPos);
  223. }
  224. void sendDragMove (DragAndDropTarget::SourceDetails& details) const
  225. {
  226. if (DragAndDropTarget* const target = getCurrentlyOver())
  227. if (target->isInterestedInDragSource (details))
  228. target->itemDragMove (details);
  229. }
  230. struct ExternalDragAndDropMessage : public CallbackMessage
  231. {
  232. ExternalDragAndDropMessage (const StringArray& f, bool canMove)
  233. : files (f), canMoveFiles (canMove)
  234. {}
  235. void messageCallback() override
  236. {
  237. DragAndDropContainer::performExternalDragDropOfFiles (files, canMoveFiles);
  238. }
  239. private:
  240. StringArray files;
  241. bool canMoveFiles;
  242. };
  243. void checkForExternalDrag (DragAndDropTarget::SourceDetails& details, Point<int> screenPos)
  244. {
  245. if (! hasCheckedForExternalDrag)
  246. {
  247. if (Desktop::getInstance().findComponentAt (screenPos) == nullptr)
  248. {
  249. hasCheckedForExternalDrag = true;
  250. StringArray files;
  251. bool canMoveFiles = false;
  252. if (owner.shouldDropFilesWhenDraggedExternally (details, files, canMoveFiles)
  253. && files.size() > 0
  254. && ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  255. {
  256. (new ExternalDragAndDropMessage (files, canMoveFiles))->post();
  257. deleteSelf();
  258. }
  259. }
  260. }
  261. }
  262. void deleteSelf()
  263. {
  264. delete this;
  265. }
  266. void dismissWithAnimation (const bool shouldSnapBack)
  267. {
  268. setVisible (true);
  269. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  270. if (shouldSnapBack && sourceDetails.sourceComponent != nullptr)
  271. {
  272. const Point<int> target (sourceDetails.sourceComponent->localPointToGlobal (sourceDetails.sourceComponent->getLocalBounds().getCentre()));
  273. const Point<int> ourCentre (localPointToGlobal (getLocalBounds().getCentre()));
  274. animator.animateComponent (this,
  275. getBounds() + (target - ourCentre),
  276. 0.0f, 120,
  277. true, 1.0, 1.0);
  278. }
  279. else
  280. {
  281. animator.fadeOut (this, 120);
  282. }
  283. }
  284. JUCE_DECLARE_NON_COPYABLE (DragImageComponent)
  285. };
  286. //==============================================================================
  287. DragAndDropContainer::DragAndDropContainer()
  288. {
  289. }
  290. DragAndDropContainer::~DragAndDropContainer()
  291. {
  292. dragImageComponent = nullptr;
  293. }
  294. void DragAndDropContainer::startDragging (const var& sourceDescription,
  295. Component* sourceComponent,
  296. Image dragImage,
  297. const bool allowDraggingToExternalWindows,
  298. const Point<int>* imageOffsetFromMouse)
  299. {
  300. if (dragImageComponent == nullptr)
  301. {
  302. MouseInputSource* const draggingSource = Desktop::getInstance().getDraggingMouseSource (0);
  303. if (draggingSource == nullptr || ! draggingSource->isDragging())
  304. {
  305. jassertfalse; // You must call startDragging() from within a mouseDown or mouseDrag callback!
  306. return;
  307. }
  308. const Point<int> lastMouseDown (draggingSource->getLastMouseDownPosition().roundToInt());
  309. Point<int> imageOffset;
  310. if (dragImage.isNull())
  311. {
  312. dragImage = sourceComponent->createComponentSnapshot (sourceComponent->getLocalBounds())
  313. .convertedToFormat (Image::ARGB);
  314. dragImage.multiplyAllAlphas (0.6f);
  315. const int lo = 150;
  316. const int hi = 400;
  317. Point<int> relPos (sourceComponent->getLocalPoint (nullptr, lastMouseDown));
  318. Point<int> clipped (dragImage.getBounds().getConstrainedPoint (relPos));
  319. Random random;
  320. for (int y = dragImage.getHeight(); --y >= 0;)
  321. {
  322. const double dy = (y - clipped.getY()) * (y - clipped.getY());
  323. for (int x = dragImage.getWidth(); --x >= 0;)
  324. {
  325. const int dx = x - clipped.getX();
  326. const int distance = roundToInt (std::sqrt (dx * dx + dy));
  327. if (distance > lo)
  328. {
  329. const float alpha = (distance > hi) ? 0
  330. : (hi - distance) / (float) (hi - lo)
  331. + random.nextFloat() * 0.008f;
  332. dragImage.multiplyAlphaAt (x, y, alpha);
  333. }
  334. }
  335. }
  336. imageOffset = clipped;
  337. }
  338. else
  339. {
  340. if (imageOffsetFromMouse == nullptr)
  341. imageOffset = dragImage.getBounds().getCentre();
  342. else
  343. imageOffset = dragImage.getBounds().getConstrainedPoint (-*imageOffsetFromMouse);
  344. }
  345. dragImageComponent = new DragImageComponent (dragImage, sourceDescription, sourceComponent,
  346. draggingSource->getComponentUnderMouse(), *this, imageOffset);
  347. if (allowDraggingToExternalWindows)
  348. {
  349. if (! Desktop::canUseSemiTransparentWindows())
  350. dragImageComponent->setOpaque (true);
  351. dragImageComponent->addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  352. | ComponentPeer::windowIsTemporary
  353. | ComponentPeer::windowIgnoresKeyPresses);
  354. }
  355. else
  356. {
  357. if (Component* const thisComp = dynamic_cast<Component*> (this))
  358. {
  359. thisComp->addChildComponent (dragImageComponent);
  360. }
  361. else
  362. {
  363. jassertfalse; // Your DragAndDropContainer needs to be a Component!
  364. return;
  365. }
  366. }
  367. static_cast<DragImageComponent*> (dragImageComponent.get())->updateLocation (false, lastMouseDown);
  368. dragImageComponent->enterModalState();
  369. #if JUCE_WINDOWS
  370. // Under heavy load, the layered window's paint callback can often be lost by the OS,
  371. // so forcing a repaint at least once makes sure that the window becomes visible..
  372. if (ComponentPeer* const peer = dragImageComponent->getPeer())
  373. peer->performAnyPendingRepaintsNow();
  374. #endif
  375. dragOperationStarted();
  376. }
  377. }
  378. bool DragAndDropContainer::isDragAndDropActive() const
  379. {
  380. return dragImageComponent != nullptr;
  381. }
  382. var DragAndDropContainer::getCurrentDragDescription() const
  383. {
  384. return dragImageComponent != nullptr ? dragImageComponent->sourceDetails.description
  385. : var();
  386. }
  387. void DragAndDropContainer::setCurrentDragImage (const Image& newImage)
  388. {
  389. if (dragImageComponent != nullptr)
  390. dragImageComponent->updateImage (newImage);
  391. }
  392. DragAndDropContainer* DragAndDropContainer::findParentDragContainerFor (Component* c)
  393. {
  394. return c != nullptr ? c->findParentComponentOfClass<DragAndDropContainer>() : nullptr;
  395. }
  396. bool DragAndDropContainer::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails&, StringArray&, bool&)
  397. {
  398. return false;
  399. }
  400. void DragAndDropContainer::dragOperationStarted() {}
  401. void DragAndDropContainer::dragOperationEnded() {}
  402. //==============================================================================
  403. DragAndDropTarget::SourceDetails::SourceDetails (const var& desc, Component* comp, Point<int> pos) noexcept
  404. : description (desc),
  405. sourceComponent (comp),
  406. localPosition (pos)
  407. {
  408. }
  409. void DragAndDropTarget::itemDragEnter (const SourceDetails&) {}
  410. void DragAndDropTarget::itemDragMove (const SourceDetails&) {}
  411. void DragAndDropTarget::itemDragExit (const SourceDetails&) {}
  412. bool DragAndDropTarget::shouldDrawDragImageWhenOver() { return true; }
  413. //==============================================================================
  414. void FileDragAndDropTarget::fileDragEnter (const StringArray&, int, int) {}
  415. void FileDragAndDropTarget::fileDragMove (const StringArray&, int, int) {}
  416. void FileDragAndDropTarget::fileDragExit (const StringArray&) {}
  417. void TextDragAndDropTarget::textDragEnter (const String&, int, int) {}
  418. void TextDragAndDropTarget::textDragMove (const String&, int, int) {}
  419. void TextDragAndDropTarget::textDragExit (const String&) {}