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.

251 lines
13KB

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