Audio plugin host https://kx.studio/carla
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.

juce_DragAndDropContainer.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Enables drag-and-drop behaviour for a component and all its sub-components.
  23. For a component to be able to make or receive drag-and-drop events, one of its parent
  24. components must derive from this class. It's probably best for the top-level
  25. component to implement it.
  26. Then to start a drag operation, any sub-component can just call the startDragging()
  27. method, and this object will take over, tracking the mouse and sending appropriate
  28. callbacks to any child components derived from DragAndDropTarget which the mouse
  29. moves over.
  30. Note: If all that you need to do is to respond to files being drag-and-dropped from
  31. the operating system onto your component, you don't need any of these classes: you can do this
  32. simply by overriding FileDragAndDropTarget::filesDropped().
  33. @see DragAndDropTarget
  34. @tags{GUI}
  35. */
  36. class JUCE_API DragAndDropContainer
  37. {
  38. public:
  39. //==============================================================================
  40. /** Creates a DragAndDropContainer.
  41. The object that derives from this class must also be a Component.
  42. */
  43. DragAndDropContainer();
  44. /** Destructor. */
  45. virtual ~DragAndDropContainer();
  46. //==============================================================================
  47. /** Begins a drag-and-drop operation.
  48. This starts a drag-and-drop operation - call it when the user drags the
  49. mouse in your drag-source component, and this object will track mouse
  50. movements until the user lets go of the mouse button, and will send
  51. appropriate messages to DragAndDropTarget objects that the mouse moves
  52. over.
  53. findParentDragContainerFor() is a handy method to call to find the
  54. drag container to use for a component.
  55. @param sourceDescription a string or value to use as the description of the thing being dragged -
  56. this will be passed to the objects that might be dropped-onto so they can
  57. decide whether they want to handle it
  58. @param sourceComponent the component that is being dragged
  59. @param dragImage the image to drag around underneath the mouse. If this is a null image,
  60. a snapshot of the sourceComponent will be used instead.
  61. @param allowDraggingToOtherJuceWindows if true, the dragged component will appear as a desktop
  62. window, and can be dragged to DragAndDropTargets that are the
  63. children of components other than this one.
  64. @param imageOffsetFromMouse if an image has been passed-in, this specifies the offset
  65. at which the image should be drawn from the mouse. If it isn't
  66. specified, then the image will be centred around the mouse. If
  67. an image hasn't been passed-in, this will be ignored.
  68. @param inputSourceCausingDrag the mouse input source which started the drag. When calling
  69. from within a mouseDown or mouseDrag event, you can pass
  70. MouseEvent::source to this method. If this param is nullptr then JUCE
  71. will use the mouse input source which is currently dragging. If there
  72. are several dragging mouse input sources (which can often occur on mobile)
  73. then JUCE will use the mouseInputSource which is closest to the sourceComponent.
  74. */
  75. void startDragging (const var& sourceDescription,
  76. Component* sourceComponent,
  77. Image dragImage = Image(),
  78. bool allowDraggingToOtherJuceWindows = false,
  79. const Point<int>* imageOffsetFromMouse = nullptr,
  80. const MouseInputSource* inputSourceCausingDrag = nullptr);
  81. /** Returns true if something is currently being dragged. */
  82. bool isDragAndDropActive() const;
  83. /** Returns the number of things currently being dragged. */
  84. int getNumCurrentDrags() const;
  85. /** Returns the description of the thing that's currently being dragged.
  86. If nothing's being dragged, this will return a null var, otherwise it'll return
  87. the var that was passed into startDragging().
  88. If you are using drag and drop in a multi-touch environment then you should use the
  89. getDragDescriptionForIndex() method instead which takes a touch index parameter.
  90. @see startDragging, getDragDescriptionForIndex
  91. */
  92. var getCurrentDragDescription() const;
  93. /** Same as the getCurrentDragDescription() method but takes a touch index parameter.
  94. @see getCurrentDragDescription
  95. */
  96. var getDragDescriptionForIndex (int index) const;
  97. /** If a drag is in progress, this allows the image being shown to be dynamically updated.
  98. If you are using drag and drop in a multi-touch environment then you should use the
  99. setDragImageForIndex() method instead which takes a touch index parameter.
  100. @see setDragImageForIndex
  101. */
  102. void setCurrentDragImage (const Image& newImage);
  103. /** Same as the setCurrentDragImage() method but takes a touch index parameter.
  104. @see setCurrentDragImage
  105. */
  106. void setDragImageForIndex (int index, const Image& newImage);
  107. /** Utility to find the DragAndDropContainer for a given Component.
  108. This will search up this component's parent hierarchy looking for the first
  109. parent component which is a DragAndDropContainer.
  110. It's useful when a component wants to call startDragging but doesn't know
  111. the DragAndDropContainer it should to use.
  112. Obviously this may return nullptr if it doesn't find a suitable component.
  113. */
  114. static DragAndDropContainer* findParentDragContainerFor (Component* childComponent);
  115. //==============================================================================
  116. /** This performs an asynchronous drag-and-drop of a set of files to some external
  117. application.
  118. You can call this function in response to a mouseDrag callback, and it will
  119. use a native operating system drag-and-drop operation to move or copy some
  120. files to another application.
  121. @param files a list of filenames to drag
  122. @param canMoveFiles if true, the app that receives the files is allowed to move the files to a new location
  123. (if this is appropriate). If false, the receiver is expected to make a copy of them.
  124. @param sourceComponent normally, JUCE will assume that the component under the mouse is the source component
  125. of the drag, but you can use this parameter to override this.
  126. @param callback an optional completion callback that will be called when the operation has ended.
  127. @returns true if the drag operation was successfully started, or false if it failed for some reason
  128. @see performExternalDragDropOfText
  129. */
  130. static bool performExternalDragDropOfFiles (const StringArray& files, bool canMoveFiles,
  131. Component* sourceComponent = nullptr,
  132. std::function<void()> callback = nullptr);
  133. /** This performs an asynchronous drag-and-drop of a block of text to some external
  134. application.
  135. You can call this function in response to a mouseDrag callback, and it will
  136. use a native operating system drag-and-drop operation to move or copy some
  137. text to another application.
  138. @param text the text to copy
  139. @param sourceComponent Normally, JUCE will assume that the component under the mouse is the source component
  140. of the drag, but you can use this parameter to override this.
  141. @param callback an optional completion callback that will be called when the operation has ended.
  142. @returns true if the drag operation was successfully started, or false if it failed for some reason
  143. @see performExternalDragDropOfFiles
  144. */
  145. static bool performExternalDragDropOfText (const String& text, Component* sourceComponent = nullptr,
  146. std::function<void()> callback = nullptr);
  147. protected:
  148. /** Override this if you want to be able to perform an external drag of a set of files
  149. when the user drags outside of this container component.
  150. This method will be called when a drag operation moves outside the JUCE window,
  151. and if you want it to then perform a file drag-and-drop, add the filenames you want
  152. to the array passed in, and return true.
  153. @param sourceDetails information about the source of the drag operation
  154. @param files on return, the filenames you want to drag
  155. @param canMoveFiles on return, true if it's ok for the receiver to move the files; false if
  156. it must make a copy of them (see the performExternalDragDropOfFiles() method)
  157. @see performExternalDragDropOfFiles, shouldDropTextWhenDraggedExternally
  158. */
  159. virtual bool shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  160. StringArray& files, bool& canMoveFiles);
  161. /** Override this if you want to be able to perform an external drag of text
  162. when the user drags outside of this container component.
  163. This method will be called when a drag operation moves outside the JUCE window,
  164. and if you want it to then perform a text drag-and-drop, copy the text you want to
  165. be dragged into the argument provided and return true.
  166. @param sourceDetails information about the source of the drag operation
  167. @param text on return, the text you want to drag
  168. @see shouldDropFilesWhenDraggedExternally
  169. */
  170. virtual bool shouldDropTextWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  171. String& text);
  172. /** Subclasses can override this to be told when a drag starts. */
  173. virtual void dragOperationStarted (const DragAndDropTarget::SourceDetails&);
  174. /** Subclasses can override this to be told when a drag finishes. */
  175. virtual void dragOperationEnded (const DragAndDropTarget::SourceDetails&);
  176. private:
  177. //==============================================================================
  178. class DragImageComponent;
  179. OwnedArray<DragImageComponent> dragImageComponents;
  180. const MouseInputSource* getMouseInputSourceForDrag (Component* sourceComponent, const MouseInputSource* inputSourceCausingDrag);
  181. bool isAlreadyDragging (Component* sourceComponent) const noexcept;
  182. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  183. // This is just here to cause a compile error in old code that hasn't been changed to use the new
  184. // version of this method.
  185. virtual int dragOperationStarted() { return 0; }
  186. virtual int dragOperationEnded() { return 0; }
  187. #endif
  188. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropContainer)
  189. };
  190. } // namespace juce