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.

471 lines
16KB

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