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.

461 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. bool juce_performDragDropFiles (const StringArray&, const bool copyFiles, bool& shouldStop);
  19. bool juce_performDragDropText (const String&, bool& shouldStop);
  20. //==============================================================================
  21. class DragAndDropContainer::DragImageComponent : public Component,
  22. private Timer
  23. {
  24. public:
  25. DragImageComponent (const Image& im,
  26. const var& desc,
  27. Component* const sourceComponent,
  28. Component* const mouseDragSource_,
  29. DragAndDropContainer& owner_,
  30. const Point<int>& imageOffset_)
  31. : sourceDetails (desc, sourceComponent, Point<int>()),
  32. image (im),
  33. owner (owner_),
  34. mouseDragSource (mouseDragSource_),
  35. imageOffset (imageOffset_),
  36. hasCheckedForExternalDrag (false)
  37. {
  38. setSize (im.getWidth(), im.getHeight());
  39. if (mouseDragSource == nullptr)
  40. mouseDragSource = sourceComponent;
  41. mouseDragSource->addMouseListener (this, false);
  42. startTimer (200);
  43. setInterceptsMouseClicks (false, false);
  44. setAlwaysOnTop (true);
  45. }
  46. ~DragImageComponent()
  47. {
  48. if (owner.dragImageComponent == this)
  49. owner.dragImageComponent.release();
  50. if (mouseDragSource != nullptr)
  51. {
  52. mouseDragSource->removeMouseListener (this);
  53. DragAndDropTarget* const current = getCurrentlyOver();
  54. if (current != nullptr && current->isInterestedInDragSource (sourceDetails))
  55. current->itemDragExit (sourceDetails);
  56. }
  57. }
  58. void paint (Graphics& g)
  59. {
  60. if (isOpaque())
  61. g.fillAll (Colours::white);
  62. g.setOpacity (1.0f);
  63. g.drawImageAt (image, 0, 0);
  64. }
  65. void mouseUp (const MouseEvent& e)
  66. {
  67. if (e.originalComponent != this)
  68. {
  69. if (mouseDragSource != nullptr)
  70. mouseDragSource->removeMouseListener (this);
  71. // (note: use a local copy of this in case the callback runs
  72. // a modal loop and deletes this object before the method completes)
  73. DragAndDropTarget::SourceDetails details (sourceDetails);
  74. DragAndDropTarget* finalTarget = nullptr;
  75. const bool wasVisible = isVisible();
  76. setVisible (false);
  77. Component* unused;
  78. finalTarget = findTarget (e.getScreenPosition(), details.localPosition, unused);
  79. if (wasVisible) // fade the component and remove it - it'll be deleted later by the timer callback
  80. dismissWithAnimation (finalTarget == nullptr);
  81. if (getParentComponent() != nullptr)
  82. getParentComponent()->removeChildComponent (this);
  83. if (finalTarget != nullptr)
  84. {
  85. currentlyOverComp = nullptr;
  86. finalTarget->itemDropped (details);
  87. }
  88. // careful - this object could now be deleted..
  89. }
  90. }
  91. void mouseDrag (const MouseEvent& e)
  92. {
  93. if (e.originalComponent != this)
  94. updateLocation (true, e.getScreenPosition());
  95. }
  96. void updateLocation (const bool canDoExternalDrag, const Point<int>& screenPos)
  97. {
  98. DragAndDropTarget::SourceDetails details (sourceDetails);
  99. setNewScreenPos (screenPos);
  100. Component* newTargetComp;
  101. DragAndDropTarget* const newTarget = findTarget (screenPos, details.localPosition, newTargetComp);
  102. setVisible (newTarget == nullptr || newTarget->shouldDrawDragImageWhenOver());
  103. if (newTargetComp != currentlyOverComp)
  104. {
  105. DragAndDropTarget* const lastTarget = getCurrentlyOver();
  106. if (lastTarget != nullptr && details.sourceComponent != nullptr
  107. && lastTarget->isInterestedInDragSource (details))
  108. lastTarget->itemDragExit (details);
  109. currentlyOverComp = newTargetComp;
  110. if (newTarget != nullptr
  111. && newTarget->isInterestedInDragSource (details))
  112. newTarget->itemDragEnter (details);
  113. }
  114. sendDragMove (details);
  115. if (canDoExternalDrag)
  116. {
  117. const Time now (Time::getCurrentTime());
  118. if (getCurrentlyOver() != nullptr)
  119. lastTimeOverTarget = now;
  120. else if (now > lastTimeOverTarget + RelativeTime::milliseconds (700))
  121. checkForExternalDrag (details, screenPos);
  122. }
  123. }
  124. void timerCallback()
  125. {
  126. if (sourceDetails.sourceComponent == nullptr)
  127. {
  128. delete this;
  129. }
  130. else if (! isMouseButtonDownAnywhere())
  131. {
  132. if (mouseDragSource != nullptr)
  133. mouseDragSource->removeMouseListener (this);
  134. delete this;
  135. }
  136. }
  137. private:
  138. DragAndDropTarget::SourceDetails sourceDetails;
  139. Image image;
  140. DragAndDropContainer& owner;
  141. WeakReference<Component> mouseDragSource, currentlyOverComp;
  142. const Point<int> imageOffset;
  143. bool hasCheckedForExternalDrag;
  144. Time lastTimeOverTarget;
  145. DragAndDropTarget* getCurrentlyOver() const noexcept
  146. {
  147. return dynamic_cast <DragAndDropTarget*> (currentlyOverComp.get());
  148. }
  149. DragAndDropTarget* findTarget (const Point<int>& screenPos, Point<int>& relativePos,
  150. Component*& resultComponent) const
  151. {
  152. Component* hit = getParentComponent();
  153. if (hit == nullptr)
  154. hit = Desktop::getInstance().findComponentAt (screenPos);
  155. else
  156. hit = hit->getComponentAt (hit->getLocalPoint (nullptr, screenPos));
  157. // (note: use a local copy of this in case the callback runs
  158. // a modal loop and deletes this object before the method completes)
  159. const DragAndDropTarget::SourceDetails details (sourceDetails);
  160. while (hit != nullptr)
  161. {
  162. DragAndDropTarget* const ddt = dynamic_cast <DragAndDropTarget*> (hit);
  163. if (ddt != nullptr && ddt->isInterestedInDragSource (details))
  164. {
  165. relativePos = hit->getLocalPoint (nullptr, screenPos);
  166. resultComponent = hit;
  167. return ddt;
  168. }
  169. hit = hit->getParentComponent();
  170. }
  171. resultComponent = nullptr;
  172. return nullptr;
  173. }
  174. void setNewScreenPos (const Point<int>& screenPos)
  175. {
  176. Point<int> newPos (screenPos - imageOffset);
  177. if (getParentComponent() != nullptr)
  178. newPos = getParentComponent()->getLocalPoint (nullptr, newPos);
  179. setTopLeftPosition (newPos);
  180. }
  181. void sendDragMove (DragAndDropTarget::SourceDetails& details) const
  182. {
  183. DragAndDropTarget* const target = getCurrentlyOver();
  184. if (target != nullptr && 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()
  193. {
  194. DragAndDropContainer::performExternalDragDropOfFiles (files, canMoveFiles);
  195. }
  196. private:
  197. StringArray files;
  198. bool canMoveFiles;
  199. };
  200. void checkForExternalDrag (DragAndDropTarget::SourceDetails& details, const 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. const Image& dragImage_,
  250. const bool allowDraggingToExternalWindows,
  251. const Point<int>* imageOffsetFromMouse)
  252. {
  253. Image dragImage (dragImage_);
  254. if (dragImageComponent == nullptr)
  255. {
  256. MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource (0);
  257. if (draggingSource == nullptr || ! draggingSource->isDragging())
  258. {
  259. jassertfalse; // You must call startDragging() from within a mouseDown or mouseDrag callback!
  260. return;
  261. }
  262. const Point<int> lastMouseDown (Desktop::getLastMouseDownPosition());
  263. Point<int> imageOffset;
  264. if (dragImage.isNull())
  265. {
  266. dragImage = sourceComponent->createComponentSnapshot (sourceComponent->getLocalBounds())
  267. .convertedToFormat (Image::ARGB);
  268. dragImage.multiplyAllAlphas (0.6f);
  269. const int lo = 150;
  270. const int hi = 400;
  271. Point<int> relPos (sourceComponent->getLocalPoint (nullptr, lastMouseDown));
  272. Point<int> clipped (dragImage.getBounds().getConstrainedPoint (relPos));
  273. Random random;
  274. for (int y = dragImage.getHeight(); --y >= 0;)
  275. {
  276. const double dy = (y - clipped.getY()) * (y - clipped.getY());
  277. for (int x = dragImage.getWidth(); --x >= 0;)
  278. {
  279. const int dx = x - clipped.getX();
  280. const int distance = roundToInt (std::sqrt (dx * dx + dy));
  281. if (distance > lo)
  282. {
  283. const float alpha = (distance > hi) ? 0
  284. : (hi - distance) / (float) (hi - lo)
  285. + random.nextFloat() * 0.008f;
  286. dragImage.multiplyAlphaAt (x, y, alpha);
  287. }
  288. }
  289. }
  290. imageOffset = clipped;
  291. }
  292. else
  293. {
  294. if (imageOffsetFromMouse == nullptr)
  295. imageOffset = dragImage.getBounds().getCentre();
  296. else
  297. imageOffset = dragImage.getBounds().getConstrainedPoint (-*imageOffsetFromMouse);
  298. }
  299. dragImageComponent = new DragImageComponent (dragImage, sourceDescription, sourceComponent,
  300. draggingSource->getComponentUnderMouse(), *this, imageOffset);
  301. currentDragDesc = sourceDescription;
  302. if (allowDraggingToExternalWindows)
  303. {
  304. if (! Desktop::canUseSemiTransparentWindows())
  305. dragImageComponent->setOpaque (true);
  306. dragImageComponent->addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  307. | ComponentPeer::windowIsTemporary
  308. | ComponentPeer::windowIgnoresKeyPresses);
  309. }
  310. else
  311. {
  312. Component* const thisComp = dynamic_cast <Component*> (this);
  313. if (thisComp == nullptr)
  314. {
  315. jassertfalse; // Your DragAndDropContainer needs to be a Component!
  316. return;
  317. }
  318. thisComp->addChildComponent (dragImageComponent);
  319. }
  320. static_cast <DragImageComponent*> (dragImageComponent.get())->updateLocation (false, lastMouseDown);
  321. dragImageComponent->setVisible (true);
  322. #if JUCE_WINDOWS
  323. // Under heavy load, the layered window's paint callback can often be lost by the OS,
  324. // so forcing a repaint at least once makes sure that the window becomes visible..
  325. if (ComponentPeer* const peer = dragImageComponent->getPeer())
  326. peer->performAnyPendingRepaintsNow();
  327. #endif
  328. }
  329. }
  330. bool DragAndDropContainer::isDragAndDropActive() const
  331. {
  332. return dragImageComponent != nullptr;
  333. }
  334. String DragAndDropContainer::getCurrentDragDescription() const
  335. {
  336. return dragImageComponent != nullptr ? currentDragDesc
  337. : String::empty;
  338. }
  339. DragAndDropContainer* DragAndDropContainer::findParentDragContainerFor (Component* c)
  340. {
  341. return c != nullptr ? c->findParentComponentOfClass<DragAndDropContainer>() : nullptr;
  342. }
  343. bool DragAndDropContainer::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails&, StringArray&, bool&)
  344. {
  345. return false;
  346. }
  347. //==============================================================================
  348. DragAndDropTarget::SourceDetails::SourceDetails (const var& description_, Component* sourceComponent_, const Point<int>& localPosition_) noexcept
  349. : description (description_),
  350. sourceComponent (sourceComponent_),
  351. localPosition (localPosition_)
  352. {
  353. }
  354. void DragAndDropTarget::itemDragEnter (const SourceDetails&) {}
  355. void DragAndDropTarget::itemDragMove (const SourceDetails&) {}
  356. void DragAndDropTarget::itemDragExit (const SourceDetails&) {}
  357. bool DragAndDropTarget::shouldDrawDragImageWhenOver() { return true; }
  358. //==============================================================================
  359. void FileDragAndDropTarget::fileDragEnter (const StringArray&, int, int) {}
  360. void FileDragAndDropTarget::fileDragMove (const StringArray&, int, int) {}
  361. void FileDragAndDropTarget::fileDragExit (const StringArray&) {}
  362. void TextDragAndDropTarget::textDragEnter (const String&, int, int) {}
  363. void TextDragAndDropTarget::textDragMove (const String&, int, int) {}
  364. void TextDragAndDropTarget::textDragExit (const String&) {}