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.

483 lines
17KB

  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 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. setSize (im.getWidth(), im.getHeight());
  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. }
  56. void paint (Graphics& g) override
  57. {
  58. if (isOpaque())
  59. g.fillAll (Colours::white);
  60. g.setOpacity (1.0f);
  61. g.drawImageAt (image, 0, 0);
  62. }
  63. void mouseUp (const MouseEvent& e) override
  64. {
  65. if (e.originalComponent != this)
  66. {
  67. if (mouseDragSource != nullptr)
  68. mouseDragSource->removeMouseListener (this);
  69. // (note: use a local copy of this in case the callback runs
  70. // a modal loop and deletes this object before the method completes)
  71. DragAndDropTarget::SourceDetails details (sourceDetails);
  72. DragAndDropTarget* finalTarget = nullptr;
  73. const bool wasVisible = isVisible();
  74. setVisible (false);
  75. Component* unused;
  76. finalTarget = findTarget (e.getScreenPosition(), details.localPosition, unused);
  77. if (wasVisible) // fade the component and remove it - it'll be deleted later by the timer callback
  78. dismissWithAnimation (finalTarget == nullptr);
  79. if (Component* parent = getParentComponent())
  80. parent->removeChildComponent (this);
  81. if (finalTarget != nullptr)
  82. {
  83. currentlyOverComp = nullptr;
  84. finalTarget->itemDropped (details);
  85. }
  86. // careful - this object could now be deleted..
  87. }
  88. }
  89. void mouseDrag (const MouseEvent& e) override
  90. {
  91. if (e.originalComponent != this)
  92. updateLocation (true, e.getScreenPosition());
  93. }
  94. void updateLocation (const bool canDoExternalDrag, Point<int> screenPos)
  95. {
  96. DragAndDropTarget::SourceDetails details (sourceDetails);
  97. setNewScreenPos (screenPos);
  98. Component* newTargetComp;
  99. DragAndDropTarget* const newTarget = findTarget (screenPos, details.localPosition, newTargetComp);
  100. setVisible (newTarget == nullptr || newTarget->shouldDrawDragImageWhenOver());
  101. if (newTargetComp != currentlyOverComp)
  102. {
  103. if (DragAndDropTarget* const lastTarget = getCurrentlyOver())
  104. if (details.sourceComponent != nullptr && lastTarget->isInterestedInDragSource (details))
  105. lastTarget->itemDragExit (details);
  106. currentlyOverComp = newTargetComp;
  107. if (newTarget != nullptr
  108. && newTarget->isInterestedInDragSource (details))
  109. newTarget->itemDragEnter (details);
  110. }
  111. sendDragMove (details);
  112. if (canDoExternalDrag)
  113. {
  114. const Time now (Time::getCurrentTime());
  115. if (getCurrentlyOver() != nullptr)
  116. lastTimeOverTarget = now;
  117. else if (now > lastTimeOverTarget + RelativeTime::milliseconds (700))
  118. checkForExternalDrag (details, screenPos);
  119. }
  120. forceMouseCursorUpdate();
  121. }
  122. void timerCallback() override
  123. {
  124. forceMouseCursorUpdate();
  125. if (sourceDetails.sourceComponent == nullptr)
  126. {
  127. delete this;
  128. }
  129. else if (! isMouseButtonDownAnywhere())
  130. {
  131. if (mouseDragSource != nullptr)
  132. mouseDragSource->removeMouseListener (this);
  133. delete this;
  134. }
  135. }
  136. bool keyPressed (const KeyPress& key) override
  137. {
  138. if (key == KeyPress::escapeKey)
  139. {
  140. dismissWithAnimation (true);
  141. delete this;
  142. return true;
  143. }
  144. return false;
  145. }
  146. bool canModalEventBeSentToComponent (const Component* targetComponent) override
  147. {
  148. return targetComponent == mouseDragSource;
  149. }
  150. // (overridden to avoid beeps when dragging)
  151. void inputAttemptWhenModal() override {}
  152. DragAndDropTarget::SourceDetails sourceDetails;
  153. private:
  154. Image image;
  155. DragAndDropContainer& owner;
  156. WeakReference<Component> mouseDragSource, currentlyOverComp;
  157. const Point<int> imageOffset;
  158. bool hasCheckedForExternalDrag;
  159. Time lastTimeOverTarget;
  160. void forceMouseCursorUpdate()
  161. {
  162. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  163. }
  164. DragAndDropTarget* getCurrentlyOver() const noexcept
  165. {
  166. return dynamic_cast <DragAndDropTarget*> (currentlyOverComp.get());
  167. }
  168. DragAndDropTarget* findTarget (Point<int> screenPos, Point<int>& relativePos,
  169. Component*& resultComponent) const
  170. {
  171. Component* hit = getParentComponent();
  172. if (hit == nullptr)
  173. hit = Desktop::getInstance().findComponentAt (screenPos);
  174. else
  175. hit = hit->getComponentAt (hit->getLocalPoint (nullptr, screenPos));
  176. // (note: use a local copy of this in case the callback runs
  177. // a modal loop and deletes this object before the method completes)
  178. const DragAndDropTarget::SourceDetails details (sourceDetails);
  179. while (hit != nullptr)
  180. {
  181. if (DragAndDropTarget* const ddt = dynamic_cast <DragAndDropTarget*> (hit))
  182. {
  183. if (ddt->isInterestedInDragSource (details))
  184. {
  185. relativePos = hit->getLocalPoint (nullptr, screenPos);
  186. resultComponent = hit;
  187. return ddt;
  188. }
  189. }
  190. hit = hit->getParentComponent();
  191. }
  192. resultComponent = nullptr;
  193. return nullptr;
  194. }
  195. void setNewScreenPos (Point<int> screenPos)
  196. {
  197. Point<int> newPos (screenPos - imageOffset);
  198. if (Component* p = getParentComponent())
  199. newPos = p->getLocalPoint (nullptr, newPos);
  200. setTopLeftPosition (newPos);
  201. }
  202. void sendDragMove (DragAndDropTarget::SourceDetails& details) const
  203. {
  204. if (DragAndDropTarget* const target = getCurrentlyOver())
  205. if (target->isInterestedInDragSource (details))
  206. target->itemDragMove (details);
  207. }
  208. struct ExternalDragAndDropMessage : public CallbackMessage
  209. {
  210. ExternalDragAndDropMessage (const StringArray& f, bool canMove)
  211. : files (f), canMoveFiles (canMove)
  212. {}
  213. void messageCallback() override
  214. {
  215. DragAndDropContainer::performExternalDragDropOfFiles (files, canMoveFiles);
  216. }
  217. private:
  218. StringArray files;
  219. bool canMoveFiles;
  220. };
  221. void checkForExternalDrag (DragAndDropTarget::SourceDetails& details, Point<int> screenPos)
  222. {
  223. if (! hasCheckedForExternalDrag)
  224. {
  225. if (Desktop::getInstance().findComponentAt (screenPos) == nullptr)
  226. {
  227. hasCheckedForExternalDrag = true;
  228. StringArray files;
  229. bool canMoveFiles = false;
  230. if (owner.shouldDropFilesWhenDraggedExternally (details, files, canMoveFiles)
  231. && files.size() > 0
  232. && ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  233. {
  234. (new ExternalDragAndDropMessage (files, canMoveFiles))->post();
  235. delete this;
  236. }
  237. }
  238. }
  239. }
  240. void dismissWithAnimation (const bool shouldSnapBack)
  241. {
  242. setVisible (true);
  243. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  244. if (shouldSnapBack && sourceDetails.sourceComponent != nullptr)
  245. {
  246. const Point<int> target (sourceDetails.sourceComponent->localPointToGlobal (sourceDetails.sourceComponent->getLocalBounds().getCentre()));
  247. const Point<int> ourCentre (localPointToGlobal (getLocalBounds().getCentre()));
  248. animator.animateComponent (this,
  249. getBounds() + (target - ourCentre),
  250. 0.0f, 120,
  251. true, 1.0, 1.0);
  252. }
  253. else
  254. {
  255. animator.fadeOut (this, 120);
  256. }
  257. }
  258. JUCE_DECLARE_NON_COPYABLE (DragImageComponent)
  259. };
  260. //==============================================================================
  261. DragAndDropContainer::DragAndDropContainer()
  262. {
  263. }
  264. DragAndDropContainer::~DragAndDropContainer()
  265. {
  266. dragImageComponent = nullptr;
  267. }
  268. void DragAndDropContainer::startDragging (const var& sourceDescription,
  269. Component* sourceComponent,
  270. Image dragImage,
  271. const bool allowDraggingToExternalWindows,
  272. const Point<int>* imageOffsetFromMouse)
  273. {
  274. if (dragImageComponent == nullptr)
  275. {
  276. MouseInputSource* const draggingSource = Desktop::getInstance().getDraggingMouseSource (0);
  277. if (draggingSource == nullptr || ! draggingSource->isDragging())
  278. {
  279. jassertfalse; // You must call startDragging() from within a mouseDown or mouseDrag callback!
  280. return;
  281. }
  282. const Point<int> lastMouseDown (draggingSource->getLastMouseDownPosition());
  283. Point<int> imageOffset;
  284. if (dragImage.isNull())
  285. {
  286. dragImage = sourceComponent->createComponentSnapshot (sourceComponent->getLocalBounds())
  287. .convertedToFormat (Image::ARGB);
  288. dragImage.multiplyAllAlphas (0.6f);
  289. const int lo = 150;
  290. const int hi = 400;
  291. Point<int> relPos (sourceComponent->getLocalPoint (nullptr, lastMouseDown));
  292. Point<int> clipped (dragImage.getBounds().getConstrainedPoint (relPos));
  293. Random random;
  294. for (int y = dragImage.getHeight(); --y >= 0;)
  295. {
  296. const double dy = (y - clipped.getY()) * (y - clipped.getY());
  297. for (int x = dragImage.getWidth(); --x >= 0;)
  298. {
  299. const int dx = x - clipped.getX();
  300. const int distance = roundToInt (std::sqrt (dx * dx + dy));
  301. if (distance > lo)
  302. {
  303. const float alpha = (distance > hi) ? 0
  304. : (hi - distance) / (float) (hi - lo)
  305. + random.nextFloat() * 0.008f;
  306. dragImage.multiplyAlphaAt (x, y, alpha);
  307. }
  308. }
  309. }
  310. imageOffset = clipped;
  311. }
  312. else
  313. {
  314. if (imageOffsetFromMouse == nullptr)
  315. imageOffset = dragImage.getBounds().getCentre();
  316. else
  317. imageOffset = dragImage.getBounds().getConstrainedPoint (-*imageOffsetFromMouse);
  318. }
  319. dragImageComponent = new DragImageComponent (dragImage, sourceDescription, sourceComponent,
  320. draggingSource->getComponentUnderMouse(), *this, imageOffset);
  321. if (allowDraggingToExternalWindows)
  322. {
  323. if (! Desktop::canUseSemiTransparentWindows())
  324. dragImageComponent->setOpaque (true);
  325. dragImageComponent->addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  326. | ComponentPeer::windowIsTemporary
  327. | ComponentPeer::windowIgnoresKeyPresses);
  328. }
  329. else
  330. {
  331. if (Component* const thisComp = dynamic_cast <Component*> (this))
  332. {
  333. thisComp->addChildComponent (dragImageComponent);
  334. }
  335. else
  336. {
  337. jassertfalse; // Your DragAndDropContainer needs to be a Component!
  338. return;
  339. }
  340. }
  341. static_cast <DragImageComponent*> (dragImageComponent.get())->updateLocation (false, lastMouseDown);
  342. dragImageComponent->setVisible (true);
  343. dragImageComponent->enterModalState();
  344. #if JUCE_WINDOWS
  345. // Under heavy load, the layered window's paint callback can often be lost by the OS,
  346. // so forcing a repaint at least once makes sure that the window becomes visible..
  347. if (ComponentPeer* const peer = dragImageComponent->getPeer())
  348. peer->performAnyPendingRepaintsNow();
  349. #endif
  350. }
  351. }
  352. bool DragAndDropContainer::isDragAndDropActive() const
  353. {
  354. return dragImageComponent != nullptr;
  355. }
  356. var DragAndDropContainer::getCurrentDragDescription() const
  357. {
  358. return dragImageComponent != nullptr ? dragImageComponent->sourceDetails.description
  359. : var();
  360. }
  361. DragAndDropContainer* DragAndDropContainer::findParentDragContainerFor (Component* c)
  362. {
  363. return c != nullptr ? c->findParentComponentOfClass<DragAndDropContainer>() : nullptr;
  364. }
  365. bool DragAndDropContainer::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails&, StringArray&, bool&)
  366. {
  367. return false;
  368. }
  369. //==============================================================================
  370. DragAndDropTarget::SourceDetails::SourceDetails (const var& desc, Component* comp, Point<int> pos) noexcept
  371. : description (desc),
  372. sourceComponent (comp),
  373. localPosition (pos)
  374. {
  375. }
  376. void DragAndDropTarget::itemDragEnter (const SourceDetails&) {}
  377. void DragAndDropTarget::itemDragMove (const SourceDetails&) {}
  378. void DragAndDropTarget::itemDragExit (const SourceDetails&) {}
  379. bool DragAndDropTarget::shouldDrawDragImageWhenOver() { return true; }
  380. //==============================================================================
  381. void FileDragAndDropTarget::fileDragEnter (const StringArray&, int, int) {}
  382. void FileDragAndDropTarget::fileDragMove (const StringArray&, int, int) {}
  383. void FileDragAndDropTarget::fileDragExit (const StringArray&) {}
  384. void TextDragAndDropTarget::textDragEnter (const String&, int, int) {}
  385. void TextDragAndDropTarget::textDragMove (const String&, int, int) {}
  386. void TextDragAndDropTarget::textDragExit (const String&) {}