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.

245 lines
12KB

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