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.

474 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. }
  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. // (overridden to avoid beeps when dragging)
  149. void inputAttemptWhenModal() override {}
  150. DragAndDropTarget::SourceDetails sourceDetails;
  151. private:
  152. Image image;
  153. DragAndDropContainer& owner;
  154. WeakReference<Component> mouseDragSource, currentlyOverComp;
  155. const Point<int> imageOffset;
  156. bool hasCheckedForExternalDrag;
  157. Time lastTimeOverTarget;
  158. DragAndDropTarget* getCurrentlyOver() const noexcept
  159. {
  160. return dynamic_cast <DragAndDropTarget*> (currentlyOverComp.get());
  161. }
  162. DragAndDropTarget* findTarget (Point<int> screenPos, Point<int>& relativePos,
  163. Component*& resultComponent) const
  164. {
  165. Component* hit = getParentComponent();
  166. if (hit == nullptr)
  167. hit = Desktop::getInstance().findComponentAt (screenPos);
  168. else
  169. hit = hit->getComponentAt (hit->getLocalPoint (nullptr, screenPos));
  170. // (note: use a local copy of this in case the callback runs
  171. // a modal loop and deletes this object before the method completes)
  172. const DragAndDropTarget::SourceDetails details (sourceDetails);
  173. while (hit != nullptr)
  174. {
  175. if (DragAndDropTarget* const ddt = dynamic_cast <DragAndDropTarget*> (hit))
  176. {
  177. if (ddt->isInterestedInDragSource (details))
  178. {
  179. relativePos = hit->getLocalPoint (nullptr, screenPos);
  180. resultComponent = hit;
  181. return ddt;
  182. }
  183. }
  184. hit = hit->getParentComponent();
  185. }
  186. resultComponent = nullptr;
  187. return nullptr;
  188. }
  189. void setNewScreenPos (Point<int> screenPos)
  190. {
  191. Point<int> newPos (screenPos - imageOffset);
  192. if (Component* p = getParentComponent())
  193. newPos = p->getLocalPoint (nullptr, newPos);
  194. setTopLeftPosition (newPos);
  195. }
  196. void sendDragMove (DragAndDropTarget::SourceDetails& details) const
  197. {
  198. if (DragAndDropTarget* const target = getCurrentlyOver())
  199. if (target->isInterestedInDragSource (details))
  200. target->itemDragMove (details);
  201. }
  202. struct ExternalDragAndDropMessage : public CallbackMessage
  203. {
  204. ExternalDragAndDropMessage (const StringArray& f, bool canMove)
  205. : files (f), canMoveFiles (canMove)
  206. {}
  207. void messageCallback() override
  208. {
  209. DragAndDropContainer::performExternalDragDropOfFiles (files, canMoveFiles);
  210. }
  211. private:
  212. StringArray files;
  213. bool canMoveFiles;
  214. };
  215. void checkForExternalDrag (DragAndDropTarget::SourceDetails& details, Point<int> screenPos)
  216. {
  217. if (! hasCheckedForExternalDrag)
  218. {
  219. if (Desktop::getInstance().findComponentAt (screenPos) == nullptr)
  220. {
  221. hasCheckedForExternalDrag = true;
  222. StringArray files;
  223. bool canMoveFiles = false;
  224. if (owner.shouldDropFilesWhenDraggedExternally (details, files, canMoveFiles)
  225. && files.size() > 0
  226. && ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  227. {
  228. (new ExternalDragAndDropMessage (files, canMoveFiles))->post();
  229. delete this;
  230. }
  231. }
  232. }
  233. }
  234. void dismissWithAnimation (const bool shouldSnapBack)
  235. {
  236. setVisible (true);
  237. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  238. if (shouldSnapBack && sourceDetails.sourceComponent != nullptr)
  239. {
  240. const Point<int> target (sourceDetails.sourceComponent->localPointToGlobal (sourceDetails.sourceComponent->getLocalBounds().getCentre()));
  241. const Point<int> ourCentre (localPointToGlobal (getLocalBounds().getCentre()));
  242. animator.animateComponent (this,
  243. getBounds() + (target - ourCentre),
  244. 0.0f, 120,
  245. true, 1.0, 1.0);
  246. }
  247. else
  248. {
  249. animator.fadeOut (this, 120);
  250. }
  251. }
  252. JUCE_DECLARE_NON_COPYABLE (DragImageComponent)
  253. };
  254. //==============================================================================
  255. DragAndDropContainer::DragAndDropContainer()
  256. {
  257. }
  258. DragAndDropContainer::~DragAndDropContainer()
  259. {
  260. dragImageComponent = nullptr;
  261. }
  262. void DragAndDropContainer::startDragging (const var& sourceDescription,
  263. Component* sourceComponent,
  264. Image dragImage,
  265. const bool allowDraggingToExternalWindows,
  266. const Point<int>* imageOffsetFromMouse)
  267. {
  268. if (dragImageComponent == nullptr)
  269. {
  270. MouseInputSource* const draggingSource = Desktop::getInstance().getDraggingMouseSource (0);
  271. if (draggingSource == nullptr || ! draggingSource->isDragging())
  272. {
  273. jassertfalse; // You must call startDragging() from within a mouseDown or mouseDrag callback!
  274. return;
  275. }
  276. const Point<int> lastMouseDown (draggingSource->getLastMouseDownPosition());
  277. Point<int> imageOffset;
  278. if (dragImage.isNull())
  279. {
  280. dragImage = sourceComponent->createComponentSnapshot (sourceComponent->getLocalBounds())
  281. .convertedToFormat (Image::ARGB);
  282. dragImage.multiplyAllAlphas (0.6f);
  283. const int lo = 150;
  284. const int hi = 400;
  285. Point<int> relPos (sourceComponent->getLocalPoint (nullptr, lastMouseDown));
  286. Point<int> clipped (dragImage.getBounds().getConstrainedPoint (relPos));
  287. Random random;
  288. for (int y = dragImage.getHeight(); --y >= 0;)
  289. {
  290. const double dy = (y - clipped.getY()) * (y - clipped.getY());
  291. for (int x = dragImage.getWidth(); --x >= 0;)
  292. {
  293. const int dx = x - clipped.getX();
  294. const int distance = roundToInt (std::sqrt (dx * dx + dy));
  295. if (distance > lo)
  296. {
  297. const float alpha = (distance > hi) ? 0
  298. : (hi - distance) / (float) (hi - lo)
  299. + random.nextFloat() * 0.008f;
  300. dragImage.multiplyAlphaAt (x, y, alpha);
  301. }
  302. }
  303. }
  304. imageOffset = clipped;
  305. }
  306. else
  307. {
  308. if (imageOffsetFromMouse == nullptr)
  309. imageOffset = dragImage.getBounds().getCentre();
  310. else
  311. imageOffset = dragImage.getBounds().getConstrainedPoint (-*imageOffsetFromMouse);
  312. }
  313. dragImageComponent = new DragImageComponent (dragImage, sourceDescription, sourceComponent,
  314. draggingSource->getComponentUnderMouse(), *this, imageOffset);
  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 ? dragImageComponent->sourceDetails.description
  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&) {}