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_DragAndDropContainer.cpp 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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 mouseDragSource_,
  28. DragAndDropContainer& owner_,
  29. Point<int> imageOffset_)
  30. : sourceDetails (desc, sourceComponent, Point<int>()),
  31. image (im),
  32. owner (owner_),
  33. mouseDragSource (mouseDragSource_),
  34. imageOffset (imageOffset_),
  35. hasCheckedForExternalDrag (false)
  36. {
  37. setSize (im.getWidth(), im.getHeight());
  38. if (mouseDragSource == nullptr)
  39. mouseDragSource = sourceComponent;
  40. mouseDragSource->addMouseListener (this, false);
  41. startTimer (200);
  42. setInterceptsMouseClicks (false, false);
  43. setAlwaysOnTop (true);
  44. }
  45. ~DragImageComponent()
  46. {
  47. if (owner.dragImageComponent == this)
  48. owner.dragImageComponent.release();
  49. if (mouseDragSource != nullptr)
  50. {
  51. mouseDragSource->removeMouseListener (this);
  52. if (DragAndDropTarget* const current = getCurrentlyOver())
  53. if (current->isInterestedInDragSource (sourceDetails))
  54. current->itemDragExit (sourceDetails);
  55. }
  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. }
  122. void timerCallback() override
  123. {
  124. if (sourceDetails.sourceComponent == nullptr)
  125. {
  126. delete this;
  127. }
  128. else if (! isMouseButtonDownAnywhere())
  129. {
  130. if (mouseDragSource != nullptr)
  131. mouseDragSource->removeMouseListener (this);
  132. delete this;
  133. }
  134. }
  135. private:
  136. DragAndDropTarget::SourceDetails sourceDetails;
  137. Image image;
  138. DragAndDropContainer& owner;
  139. WeakReference<Component> mouseDragSource, currentlyOverComp;
  140. const Point<int> imageOffset;
  141. bool hasCheckedForExternalDrag;
  142. Time lastTimeOverTarget;
  143. DragAndDropTarget* getCurrentlyOver() const noexcept
  144. {
  145. return dynamic_cast <DragAndDropTarget*> (currentlyOverComp.get());
  146. }
  147. DragAndDropTarget* findTarget (Point<int> screenPos, Point<int>& relativePos,
  148. Component*& resultComponent) const
  149. {
  150. Component* hit = getParentComponent();
  151. if (hit == nullptr)
  152. hit = Desktop::getInstance().findComponentAt (screenPos);
  153. else
  154. hit = hit->getComponentAt (hit->getLocalPoint (nullptr, screenPos));
  155. // (note: use a local copy of this in case the callback runs
  156. // a modal loop and deletes this object before the method completes)
  157. const DragAndDropTarget::SourceDetails details (sourceDetails);
  158. while (hit != nullptr)
  159. {
  160. if (DragAndDropTarget* const ddt = dynamic_cast <DragAndDropTarget*> (hit))
  161. {
  162. if (ddt->isInterestedInDragSource (details))
  163. {
  164. relativePos = hit->getLocalPoint (nullptr, screenPos);
  165. resultComponent = hit;
  166. return ddt;
  167. }
  168. }
  169. hit = hit->getParentComponent();
  170. }
  171. resultComponent = nullptr;
  172. return nullptr;
  173. }
  174. void setNewScreenPos (Point<int> screenPos)
  175. {
  176. Point<int> newPos (screenPos - imageOffset);
  177. if (Component* p = getParentComponent())
  178. newPos = p->getLocalPoint (nullptr, newPos);
  179. setTopLeftPosition (newPos);
  180. }
  181. void sendDragMove (DragAndDropTarget::SourceDetails& details) const
  182. {
  183. if (DragAndDropTarget* const target = getCurrentlyOver())
  184. if (target->isInterestedInDragSource (details))
  185. target->itemDragMove (details);
  186. }
  187. struct ExternalDragAndDropMessage : public CallbackMessage
  188. {
  189. ExternalDragAndDropMessage (const StringArray& f, bool canMove)
  190. : files (f), canMoveFiles (canMove)
  191. {}
  192. void messageCallback() override
  193. {
  194. DragAndDropContainer::performExternalDragDropOfFiles (files, canMoveFiles);
  195. }
  196. private:
  197. StringArray files;
  198. bool canMoveFiles;
  199. };
  200. void checkForExternalDrag (DragAndDropTarget::SourceDetails& details, Point<int> screenPos)
  201. {
  202. if (! hasCheckedForExternalDrag)
  203. {
  204. if (Desktop::getInstance().findComponentAt (screenPos) == nullptr)
  205. {
  206. hasCheckedForExternalDrag = true;
  207. StringArray files;
  208. bool canMoveFiles = false;
  209. if (owner.shouldDropFilesWhenDraggedExternally (details, files, canMoveFiles)
  210. && files.size() > 0
  211. && ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  212. {
  213. (new ExternalDragAndDropMessage (files, canMoveFiles))->post();
  214. delete this;
  215. }
  216. }
  217. }
  218. }
  219. void dismissWithAnimation (const bool shouldSnapBack)
  220. {
  221. setVisible (true);
  222. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  223. if (shouldSnapBack && sourceDetails.sourceComponent != nullptr)
  224. {
  225. const Point<int> target (sourceDetails.sourceComponent->localPointToGlobal (sourceDetails.sourceComponent->getLocalBounds().getCentre()));
  226. const Point<int> ourCentre (localPointToGlobal (getLocalBounds().getCentre()));
  227. animator.animateComponent (this,
  228. getBounds() + (target - ourCentre),
  229. 0.0f, 120,
  230. true, 1.0, 1.0);
  231. }
  232. else
  233. {
  234. animator.fadeOut (this, 120);
  235. }
  236. }
  237. JUCE_DECLARE_NON_COPYABLE (DragImageComponent)
  238. };
  239. //==============================================================================
  240. DragAndDropContainer::DragAndDropContainer()
  241. {
  242. }
  243. DragAndDropContainer::~DragAndDropContainer()
  244. {
  245. dragImageComponent = nullptr;
  246. }
  247. void DragAndDropContainer::startDragging (const var& sourceDescription,
  248. Component* sourceComponent,
  249. Image dragImage,
  250. const bool allowDraggingToExternalWindows,
  251. const Point<int>* imageOffsetFromMouse)
  252. {
  253. if (dragImageComponent == nullptr)
  254. {
  255. MouseInputSource* const draggingSource = Desktop::getInstance().getDraggingMouseSource (0);
  256. if (draggingSource == nullptr || ! draggingSource->isDragging())
  257. {
  258. jassertfalse; // You must call startDragging() from within a mouseDown or mouseDrag callback!
  259. return;
  260. }
  261. const Point<int> lastMouseDown (draggingSource->getLastMouseDownPosition());
  262. Point<int> imageOffset;
  263. if (dragImage.isNull())
  264. {
  265. dragImage = sourceComponent->createComponentSnapshot (sourceComponent->getLocalBounds())
  266. .convertedToFormat (Image::ARGB);
  267. dragImage.multiplyAllAlphas (0.6f);
  268. const int lo = 150;
  269. const int hi = 400;
  270. Point<int> relPos (sourceComponent->getLocalPoint (nullptr, lastMouseDown));
  271. Point<int> clipped (dragImage.getBounds().getConstrainedPoint (relPos));
  272. Random random;
  273. for (int y = dragImage.getHeight(); --y >= 0;)
  274. {
  275. const double dy = (y - clipped.getY()) * (y - clipped.getY());
  276. for (int x = dragImage.getWidth(); --x >= 0;)
  277. {
  278. const int dx = x - clipped.getX();
  279. const int distance = roundToInt (std::sqrt (dx * dx + dy));
  280. if (distance > lo)
  281. {
  282. const float alpha = (distance > hi) ? 0
  283. : (hi - distance) / (float) (hi - lo)
  284. + random.nextFloat() * 0.008f;
  285. dragImage.multiplyAlphaAt (x, y, alpha);
  286. }
  287. }
  288. }
  289. imageOffset = clipped;
  290. }
  291. else
  292. {
  293. if (imageOffsetFromMouse == nullptr)
  294. imageOffset = dragImage.getBounds().getCentre();
  295. else
  296. imageOffset = dragImage.getBounds().getConstrainedPoint (-*imageOffsetFromMouse);
  297. }
  298. dragImageComponent = new DragImageComponent (dragImage, sourceDescription, sourceComponent,
  299. draggingSource->getComponentUnderMouse(), *this, imageOffset);
  300. currentDragDesc = sourceDescription;
  301. if (allowDraggingToExternalWindows)
  302. {
  303. if (! Desktop::canUseSemiTransparentWindows())
  304. dragImageComponent->setOpaque (true);
  305. dragImageComponent->addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  306. | ComponentPeer::windowIsTemporary
  307. | ComponentPeer::windowIgnoresKeyPresses);
  308. }
  309. else
  310. {
  311. if (Component* const thisComp = dynamic_cast <Component*> (this))
  312. {
  313. thisComp->addChildComponent (dragImageComponent);
  314. }
  315. else
  316. {
  317. jassertfalse; // Your DragAndDropContainer needs to be a Component!
  318. return;
  319. }
  320. }
  321. static_cast <DragImageComponent*> (dragImageComponent.get())->updateLocation (false, lastMouseDown);
  322. dragImageComponent->setVisible (true);
  323. #if JUCE_WINDOWS
  324. // Under heavy load, the layered window's paint callback can often be lost by the OS,
  325. // so forcing a repaint at least once makes sure that the window becomes visible..
  326. if (ComponentPeer* const peer = dragImageComponent->getPeer())
  327. peer->performAnyPendingRepaintsNow();
  328. #endif
  329. }
  330. }
  331. bool DragAndDropContainer::isDragAndDropActive() const
  332. {
  333. return dragImageComponent != nullptr;
  334. }
  335. var DragAndDropContainer::getCurrentDragDescription() const
  336. {
  337. return dragImageComponent != nullptr ? currentDragDesc
  338. : var();
  339. }
  340. DragAndDropContainer* DragAndDropContainer::findParentDragContainerFor (Component* c)
  341. {
  342. return c != nullptr ? c->findParentComponentOfClass<DragAndDropContainer>() : nullptr;
  343. }
  344. bool DragAndDropContainer::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails&, StringArray&, bool&)
  345. {
  346. return false;
  347. }
  348. //==============================================================================
  349. DragAndDropTarget::SourceDetails::SourceDetails (const var& desc, Component* comp, Point<int> pos) noexcept
  350. : description (desc),
  351. sourceComponent (comp),
  352. localPosition (pos)
  353. {
  354. }
  355. void DragAndDropTarget::itemDragEnter (const SourceDetails&) {}
  356. void DragAndDropTarget::itemDragMove (const SourceDetails&) {}
  357. void DragAndDropTarget::itemDragExit (const SourceDetails&) {}
  358. bool DragAndDropTarget::shouldDrawDragImageWhenOver() { return true; }
  359. //==============================================================================
  360. void FileDragAndDropTarget::fileDragEnter (const StringArray&, int, int) {}
  361. void FileDragAndDropTarget::fileDragMove (const StringArray&, int, int) {}
  362. void FileDragAndDropTarget::fileDragExit (const StringArray&) {}
  363. void TextDragAndDropTarget::textDragEnter (const String&, int, int) {}
  364. void TextDragAndDropTarget::textDragMove (const String&, int, int) {}
  365. void TextDragAndDropTarget::textDragExit (const String&) {}