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.

217 lines
11KB

  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. */
  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. */
  69. void startDragging (const var& sourceDescription,
  70. Component* sourceComponent,
  71. Image dragImage = Image(),
  72. bool allowDraggingToOtherJuceWindows = false,
  73. const Point<int>* imageOffsetFromMouse = nullptr);
  74. /** Returns true if something is currently being dragged. */
  75. bool isDragAndDropActive() const;
  76. /** Returns the description of the thing that's currently being dragged.
  77. If nothing's being dragged, this will return a null var, otherwise it'll return
  78. the var that was passed into startDragging().
  79. @see startDragging
  80. */
  81. var getCurrentDragDescription() const;
  82. /** If a drag is in progress, this allows the image being shown to be dynamically updated. */
  83. void setCurrentDragImage (const Image& newImage);
  84. /** Utility to find the DragAndDropContainer for a given Component.
  85. This will search up this component's parent hierarchy looking for the first
  86. parent component which is a DragAndDropContainer.
  87. It's useful when a component wants to call startDragging but doesn't know
  88. the DragAndDropContainer it should to use.
  89. Obviously this may return nullptr if it doesn't find a suitable component.
  90. */
  91. static DragAndDropContainer* findParentDragContainerFor (Component* childComponent);
  92. //==============================================================================
  93. /** This performs a synchronous drag-and-drop of a set of files to some external
  94. application.
  95. You can call this function in response to a mouseDrag callback, and it will
  96. block, running its own internal message loop and tracking the mouse, while it
  97. uses a native operating system drag-and-drop operation to move or copy some
  98. files to another application.
  99. @param files a list of filenames to drag
  100. @param canMoveFiles if true, the app that receives the files is allowed to move the files to a new location
  101. (if this is appropriate). If false, the receiver is expected to make a copy of them.
  102. @param sourceComponent Normally, JUCE will assume that the component under the mouse is the source component
  103. of the drag, but you can use this parameter to override this.
  104. @returns true if the files were successfully dropped somewhere, or false if it
  105. was interrupted
  106. @see performExternalDragDropOfText
  107. */
  108. static bool performExternalDragDropOfFiles (const StringArray& files, bool canMoveFiles,
  109. Component* sourceComponent = nullptr);
  110. /** This performs a synchronous drag-and-drop of a block of text to some external
  111. application.
  112. You can call this function in response to a mouseDrag callback, and it will
  113. block, running its own internal message loop and tracking the mouse, while it
  114. uses a native operating system drag-and-drop operation to move or copy some
  115. text to another application.
  116. @param text the text to copy
  117. @param sourceComponent Normally, JUCE will assume that the component under the mouse is the source component
  118. of the drag, but you can use this parameter to override this.
  119. @returns true if the text was successfully dropped somewhere, or false if it
  120. was interrupted
  121. @see performExternalDragDropOfFiles
  122. */
  123. static bool performExternalDragDropOfText (const String& text, Component* sourceComponent = nullptr);
  124. protected:
  125. /** Override this if you want to be able to perform an external drag of a set of files
  126. when the user drags outside of this container component.
  127. This method will be called when a drag operation moves outside the JUCE window,
  128. and if you want it to then perform a file drag-and-drop, add the filenames you want
  129. to the array passed in, and return true.
  130. @param sourceDetails information about the source of the drag operation
  131. @param files on return, the filenames you want to drag
  132. @param canMoveFiles on return, true if it's ok for the receiver to move the files; false if
  133. it must make a copy of them (see the performExternalDragDropOfFiles() method)
  134. @see performExternalDragDropOfFiles, shouldDropTextWhenDraggedExternally
  135. */
  136. virtual bool shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  137. StringArray& files, bool& canMoveFiles);
  138. /** Override this if you want to be able to perform an external drag of text
  139. when the user drags outside of this container component.
  140. This method will be called when a drag operation moves outside the JUCE window,
  141. and if you want it to then perform a text drag-and-drop, copy the text you want to
  142. be dragged into the argument provided and return true.
  143. @param sourceDetails information about the source of the drag operation
  144. @param text on return, the text you want to drag
  145. @see shouldDropFilesWhenDraggedExternally
  146. */
  147. virtual bool shouldDropTextWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  148. String& text);
  149. /** Subclasses can override this to be told when a drag starts. */
  150. virtual void dragOperationStarted (const DragAndDropTarget::SourceDetails&);
  151. /** Subclasses can override this to be told when a drag finishes. */
  152. virtual void dragOperationEnded (const DragAndDropTarget::SourceDetails&);
  153. private:
  154. //==============================================================================
  155. class DragImageComponent;
  156. friend class DragImageComponent;
  157. friend struct ContainerDeletePolicy<DragImageComponent>;
  158. ScopedPointer<DragImageComponent> dragImageComponent;
  159. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  160. // This is just here to cause a compile error in old code that hasn't been changed to use the new
  161. // version of this method.
  162. virtual int dragOperationStarted() { return 0; }
  163. virtual int dragOperationEnded() { return 0; }
  164. #endif
  165. JUCE_DEPRECATED_WITH_BODY (virtual bool shouldDropFilesWhenDraggedExternally (const String&, Component*, StringArray&, bool&), { return false; })
  166. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropContainer)
  167. };
  168. } // namespace juce