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.

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