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.

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