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.

443 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. BEGIN_JUCE_NAMESPACE
  19. bool juce_performDragDropFiles (const StringArray&, const bool copyFiles, bool& shouldStop);
  20. bool juce_performDragDropText (const String&, bool& shouldStop);
  21. //==============================================================================
  22. class DragImageComponent : public Component,
  23. public Timer
  24. {
  25. public:
  26. DragImageComponent (const Image& im,
  27. const var& desc,
  28. Component* const sourceComponent,
  29. Component* const mouseDragSource_,
  30. DragAndDropContainer& owner_,
  31. const Point<int>& imageOffset_)
  32. : sourceDetails (desc, sourceComponent, Point<int>()),
  33. image (im),
  34. owner (owner_),
  35. mouseDragSource (mouseDragSource_),
  36. imageOffset (imageOffset_),
  37. hasCheckedForExternalDrag (false),
  38. isDoingExternalDrag (false)
  39. {
  40. setSize (im.getWidth(), im.getHeight());
  41. if (mouseDragSource == nullptr)
  42. mouseDragSource = sourceComponent;
  43. mouseDragSource->addMouseListener (this, false);
  44. startTimer (200);
  45. setInterceptsMouseClicks (false, false);
  46. setAlwaysOnTop (true);
  47. }
  48. ~DragImageComponent()
  49. {
  50. if (owner.dragImageComponent == this)
  51. owner.dragImageComponent.release();
  52. if (mouseDragSource != nullptr)
  53. {
  54. mouseDragSource->removeMouseListener (this);
  55. DragAndDropTarget* const current = getCurrentlyOver();
  56. if (current != nullptr && current->isInterestedInDragSource (sourceDetails))
  57. current->itemDragExit (sourceDetails);
  58. }
  59. }
  60. void paint (Graphics& g)
  61. {
  62. if (isOpaque())
  63. g.fillAll (Colours::white);
  64. g.setOpacity (1.0f);
  65. g.drawImageAt (image, 0, 0);
  66. }
  67. void mouseUp (const MouseEvent& e)
  68. {
  69. if (e.originalComponent != this)
  70. {
  71. if (mouseDragSource != nullptr)
  72. mouseDragSource->removeMouseListener (this);
  73. // (note: use a local copy of this in case the callback runs
  74. // a modal loop and deletes this object before the method completes)
  75. DragAndDropTarget::SourceDetails details (sourceDetails);
  76. DragAndDropTarget* finalTarget = nullptr;
  77. if (! isDoingExternalDrag)
  78. {
  79. const bool wasVisible = isVisible();
  80. setVisible (false);
  81. finalTarget = findTarget (e.getScreenPosition(), details.localPosition);
  82. if (wasVisible) // fade the component and remove it - it'll be deleted later by the timer callback
  83. dismissWithAnimation (finalTarget == nullptr);
  84. }
  85. if (getParentComponent() != nullptr)
  86. getParentComponent()->removeChildComponent (this);
  87. if (finalTarget != nullptr)
  88. {
  89. currentlyOverComp = nullptr;
  90. finalTarget->itemDropped (details);
  91. }
  92. // careful - this object could now be deleted..
  93. }
  94. }
  95. void mouseDrag (const MouseEvent& e)
  96. {
  97. if (e.originalComponent != this)
  98. updateLocation (true, e.getScreenPosition());
  99. }
  100. void updateLocation (const bool canDoExternalDrag, const Point<int>& screenPos)
  101. {
  102. DragAndDropTarget::SourceDetails details (sourceDetails);
  103. setNewScreenPos (screenPos);
  104. DragAndDropTarget* const newTarget = findTarget (screenPos, details.localPosition);
  105. Component* newTargetComp = dynamic_cast <Component*> (newTarget);
  106. setVisible (newTarget == nullptr || newTarget->shouldDrawDragImageWhenOver());
  107. if (newTargetComp != currentlyOverComp)
  108. {
  109. DragAndDropTarget* const lastTarget = getCurrentlyOver();
  110. if (lastTarget != nullptr && details.sourceComponent != nullptr
  111. && lastTarget->isInterestedInDragSource (details))
  112. lastTarget->itemDragExit (details);
  113. currentlyOverComp = newTargetComp;
  114. if (newTarget != nullptr
  115. && newTarget->isInterestedInDragSource (details))
  116. newTarget->itemDragEnter (details);
  117. }
  118. sendDragMove (details);
  119. if (canDoExternalDrag && getCurrentlyOver() == nullptr)
  120. checkForExternalDrag (details, screenPos);
  121. }
  122. void timerCallback()
  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. private:
  136. DragAndDropTarget::SourceDetails sourceDetails;
  137. Image image;
  138. DragAndDropContainer& owner;
  139. WeakReference<Component> mouseDragSource, currentlyOverComp;
  140. const Point<int> imageOffset;
  141. bool hasCheckedForExternalDrag, isDoingExternalDrag;
  142. DragAndDropTarget* getCurrentlyOver() const noexcept
  143. {
  144. return dynamic_cast <DragAndDropTarget*> (currentlyOverComp.get());
  145. }
  146. DragAndDropTarget* findTarget (const Point<int>& screenPos, Point<int>& relativePos) const
  147. {
  148. Component* hit = getParentComponent();
  149. if (hit == nullptr)
  150. hit = Desktop::getInstance().findComponentAt (screenPos);
  151. else
  152. hit = hit->getComponentAt (hit->getLocalPoint (nullptr, screenPos));
  153. // (note: use a local copy of this in case the callback runs
  154. // a modal loop and deletes this object before the method completes)
  155. const DragAndDropTarget::SourceDetails details (sourceDetails);
  156. while (hit != nullptr)
  157. {
  158. DragAndDropTarget* const ddt = dynamic_cast <DragAndDropTarget*> (hit);
  159. if (ddt != nullptr && ddt->isInterestedInDragSource (details))
  160. {
  161. relativePos = hit->getLocalPoint (nullptr, screenPos);
  162. return ddt;
  163. }
  164. hit = hit->getParentComponent();
  165. }
  166. return nullptr;
  167. }
  168. void setNewScreenPos (const Point<int>& screenPos)
  169. {
  170. Point<int> newPos (screenPos - imageOffset);
  171. if (getParentComponent() != nullptr)
  172. newPos = getParentComponent()->getLocalPoint (nullptr, newPos);
  173. setTopLeftPosition (newPos);
  174. }
  175. void sendDragMove (DragAndDropTarget::SourceDetails& details) const
  176. {
  177. DragAndDropTarget* const target = getCurrentlyOver();
  178. if (target != nullptr && target->isInterestedInDragSource (details))
  179. target->itemDragMove (details);
  180. }
  181. void checkForExternalDrag (DragAndDropTarget::SourceDetails& details, const Point<int>& screenPos)
  182. {
  183. if (! hasCheckedForExternalDrag)
  184. {
  185. if (Desktop::getInstance().findComponentAt (screenPos) == nullptr)
  186. {
  187. hasCheckedForExternalDrag = true;
  188. StringArray files;
  189. bool canMoveFiles = false;
  190. if (owner.shouldDropFilesWhenDraggedExternally (details, files, canMoveFiles)
  191. && files.size() > 0)
  192. {
  193. WeakReference<Component> thisWeakRef (this);
  194. setVisible (false);
  195. isDoingExternalDrag = true;
  196. if (ModifierKeys::getCurrentModifiersRealtime().isAnyMouseButtonDown())
  197. DragAndDropContainer::performExternalDragDropOfFiles (files, canMoveFiles);
  198. delete thisWeakRef.get();
  199. return;
  200. }
  201. }
  202. }
  203. }
  204. void dismissWithAnimation (const bool shouldSnapBack)
  205. {
  206. setVisible (true);
  207. ComponentAnimator& animator = Desktop::getInstance().getAnimator();
  208. if (shouldSnapBack && sourceDetails.sourceComponent != nullptr)
  209. {
  210. const Point<int> target (sourceDetails.sourceComponent->localPointToGlobal (sourceDetails.sourceComponent->getLocalBounds().getCentre()));
  211. const Point<int> ourCentre (localPointToGlobal (getLocalBounds().getCentre()));
  212. animator.animateComponent (this,
  213. getBounds() + (target - ourCentre),
  214. 0.0f, 120,
  215. true, 1.0, 1.0);
  216. }
  217. else
  218. {
  219. animator.fadeOut (this, 120);
  220. }
  221. }
  222. JUCE_DECLARE_NON_COPYABLE (DragImageComponent);
  223. };
  224. //==============================================================================
  225. DragAndDropContainer::DragAndDropContainer()
  226. {
  227. }
  228. DragAndDropContainer::~DragAndDropContainer()
  229. {
  230. dragImageComponent = nullptr;
  231. }
  232. void DragAndDropContainer::startDragging (const var& sourceDescription,
  233. Component* sourceComponent,
  234. const Image& dragImage_,
  235. const bool allowDraggingToExternalWindows,
  236. const Point<int>* imageOffsetFromMouse)
  237. {
  238. Image dragImage (dragImage_);
  239. if (dragImageComponent == nullptr)
  240. {
  241. Component* const thisComp = dynamic_cast <Component*> (this);
  242. if (thisComp == nullptr)
  243. {
  244. jassertfalse; // Your DragAndDropContainer needs to be a Component!
  245. return;
  246. }
  247. MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource (0);
  248. if (draggingSource == nullptr || ! draggingSource->isDragging())
  249. {
  250. jassertfalse; // You must call startDragging() from within a mouseDown or mouseDrag callback!
  251. return;
  252. }
  253. const Point<int> lastMouseDown (Desktop::getLastMouseDownPosition());
  254. Point<int> imageOffset;
  255. if (dragImage.isNull())
  256. {
  257. dragImage = sourceComponent->createComponentSnapshot (sourceComponent->getLocalBounds())
  258. .convertedToFormat (Image::ARGB);
  259. dragImage.multiplyAllAlphas (0.6f);
  260. const int lo = 150;
  261. const int hi = 400;
  262. Point<int> relPos (sourceComponent->getLocalPoint (nullptr, lastMouseDown));
  263. Point<int> clipped (dragImage.getBounds().getConstrainedPoint (relPos));
  264. Random random;
  265. for (int y = dragImage.getHeight(); --y >= 0;)
  266. {
  267. const double dy = (y - clipped.getY()) * (y - clipped.getY());
  268. for (int x = dragImage.getWidth(); --x >= 0;)
  269. {
  270. const int dx = x - clipped.getX();
  271. const int distance = roundToInt (std::sqrt (dx * dx + dy));
  272. if (distance > lo)
  273. {
  274. const float alpha = (distance > hi) ? 0
  275. : (hi - distance) / (float) (hi - lo)
  276. + random.nextFloat() * 0.008f;
  277. dragImage.multiplyAlphaAt (x, y, alpha);
  278. }
  279. }
  280. }
  281. imageOffset = clipped;
  282. }
  283. else
  284. {
  285. if (imageOffsetFromMouse == nullptr)
  286. imageOffset = dragImage.getBounds().getCentre();
  287. else
  288. imageOffset = dragImage.getBounds().getConstrainedPoint (-*imageOffsetFromMouse);
  289. }
  290. dragImageComponent = new DragImageComponent (dragImage, sourceDescription, sourceComponent,
  291. draggingSource->getComponentUnderMouse(), *this, imageOffset);
  292. currentDragDesc = sourceDescription;
  293. if (allowDraggingToExternalWindows)
  294. {
  295. if (! Desktop::canUseSemiTransparentWindows())
  296. dragImageComponent->setOpaque (true);
  297. dragImageComponent->addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  298. | ComponentPeer::windowIsTemporary
  299. | ComponentPeer::windowIgnoresKeyPresses);
  300. }
  301. else
  302. thisComp->addChildComponent (dragImageComponent);
  303. static_cast <DragImageComponent*> (dragImageComponent.get())->updateLocation (false, lastMouseDown);
  304. dragImageComponent->setVisible (true);
  305. #if JUCE_WINDOWS
  306. // Under heavy load, the layered window's paint callback can often be lost by the OS,
  307. // so forcing a repaint at least once makes sure that the window becomes visible..
  308. ComponentPeer* const peer = dragImageComponent->getPeer();
  309. if (peer != nullptr)
  310. peer->performAnyPendingRepaintsNow();
  311. #endif
  312. }
  313. }
  314. bool DragAndDropContainer::isDragAndDropActive() const
  315. {
  316. return dragImageComponent != nullptr;
  317. }
  318. String DragAndDropContainer::getCurrentDragDescription() const
  319. {
  320. return dragImageComponent != nullptr ? currentDragDesc
  321. : String::empty;
  322. }
  323. DragAndDropContainer* DragAndDropContainer::findParentDragContainerFor (Component* c)
  324. {
  325. return c == nullptr ? nullptr : c->findParentComponentOfClass ((DragAndDropContainer*) nullptr);
  326. }
  327. bool DragAndDropContainer::shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails&, StringArray&, bool&)
  328. {
  329. return false;
  330. }
  331. //==============================================================================
  332. DragAndDropTarget::SourceDetails::SourceDetails (const var& description_, Component* sourceComponent_, const Point<int>& localPosition_) noexcept
  333. : description (description_),
  334. sourceComponent (sourceComponent_),
  335. localPosition (localPosition_)
  336. {
  337. }
  338. void DragAndDropTarget::itemDragEnter (const SourceDetails&) {}
  339. void DragAndDropTarget::itemDragMove (const SourceDetails&) {}
  340. void DragAndDropTarget::itemDragExit (const SourceDetails&) {}
  341. bool DragAndDropTarget::shouldDrawDragImageWhenOver() { return true; }
  342. //==============================================================================
  343. void FileDragAndDropTarget::fileDragEnter (const StringArray&, int, int) {}
  344. void FileDragAndDropTarget::fileDragMove (const StringArray&, int, int) {}
  345. void FileDragAndDropTarget::fileDragExit (const StringArray&) {}
  346. END_JUCE_NAMESPACE