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.

191 lines
9.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_DRAGANDDROPCONTAINER_H_INCLUDED
  18. #define JUCE_DRAGANDDROPCONTAINER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Enables drag-and-drop behaviour for a component and all its sub-components.
  22. For a component to be able to make or receive drag-and-drop events, one of its parent
  23. components must derive from this class. It's probably best for the top-level
  24. component to implement it.
  25. Then to start a drag operation, any sub-component can just call the startDragging()
  26. method, and this object will take over, tracking the mouse and sending appropriate
  27. callbacks to any child components derived from DragAndDropTarget which the mouse
  28. moves over.
  29. Note: If all that you need to do is to respond to files being drag-and-dropped from
  30. the operating system onto your component, you don't need any of these classes: you can do this
  31. simply by overriding Component::filesDropped().
  32. @see DragAndDropTarget
  33. */
  34. class JUCE_API DragAndDropContainer
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates a DragAndDropContainer.
  39. The object that derives from this class must also be a Component.
  40. */
  41. DragAndDropContainer();
  42. /** Destructor. */
  43. virtual ~DragAndDropContainer();
  44. //==============================================================================
  45. /** Begins a drag-and-drop operation.
  46. This starts a drag-and-drop operation - call it when the user drags the
  47. mouse in your drag-source component, and this object will track mouse
  48. movements until the user lets go of the mouse button, and will send
  49. appropriate messages to DragAndDropTarget objects that the mouse moves
  50. over.
  51. findParentDragContainerFor() is a handy method to call to find the
  52. drag container to use for a component.
  53. @param sourceDescription a string or value to use as the description of the thing being dragged -
  54. this will be passed to the objects that might be dropped-onto so they can
  55. decide whether they want to handle it
  56. @param sourceComponent the component that is being dragged
  57. @param dragImage the image to drag around underneath the mouse. If this is a null image,
  58. a snapshot of the sourceComponent will be used instead.
  59. @param allowDraggingToOtherJuceWindows if true, the dragged component will appear as a desktop
  60. window, and can be dragged to DragAndDropTargets that are the
  61. children of components other than this one.
  62. @param imageOffsetFromMouse if an image has been passed-in, this specifies the offset
  63. at which the image should be drawn from the mouse. If it isn't
  64. specified, then the image will be centred around the mouse. If
  65. an image hasn't been passed-in, this will be ignored.
  66. */
  67. void startDragging (const var& sourceDescription,
  68. Component* sourceComponent,
  69. Image dragImage = Image::null,
  70. bool allowDraggingToOtherJuceWindows = false,
  71. const Point<int>* imageOffsetFromMouse = nullptr);
  72. /** Returns true if something is currently being dragged. */
  73. bool isDragAndDropActive() const;
  74. /** Returns the description of the thing that's currently being dragged.
  75. If nothing's being dragged, this will return a null var, otherwise it'll return
  76. the var that was passed into startDragging().
  77. @see startDragging
  78. */
  79. var getCurrentDragDescription() const;
  80. /** If a drag is in progress, this allows the image being shown to be dynamically updated. */
  81. void setCurrentDragImage (const Image& newImage);
  82. /** Utility to find the DragAndDropContainer for a given Component.
  83. This will search up this component's parent hierarchy looking for the first
  84. parent component which is a DragAndDropContainer.
  85. It's useful when a component wants to call startDragging but doesn't know
  86. the DragAndDropContainer it should to use.
  87. Obviously this may return nullptr if it doesn't find a suitable component.
  88. */
  89. static DragAndDropContainer* findParentDragContainerFor (Component* childComponent);
  90. //==============================================================================
  91. /** This performs a synchronous drag-and-drop of a set of files to some external
  92. application.
  93. You can call this function in response to a mouseDrag callback, and it will
  94. block, running its own internal message loop and tracking the mouse, while it
  95. uses a native operating system drag-and-drop operation to move or copy some
  96. files to another application.
  97. @param files a list of filenames to drag
  98. @param canMoveFiles if true, the app that receives the files is allowed to move the files to a new location
  99. (if this is appropriate). If false, the receiver is expected to make a copy of them.
  100. @returns true if the files were successfully dropped somewhere, or false if it
  101. was interrupted
  102. @see performExternalDragDropOfText
  103. */
  104. static bool performExternalDragDropOfFiles (const StringArray& files, bool canMoveFiles);
  105. /** This performs a synchronous drag-and-drop of a block of text to some external
  106. application.
  107. You can call this function in response to a mouseDrag callback, and it will
  108. block, running its own internal message loop and tracking the mouse, while it
  109. uses a native operating system drag-and-drop operation to move or copy some
  110. text to another application.
  111. @param text the text to copy
  112. @returns true if the text was successfully dropped somewhere, or false if it
  113. was interrupted
  114. @see performExternalDragDropOfFiles
  115. */
  116. static bool performExternalDragDropOfText (const String& text);
  117. protected:
  118. /** Override this if you want to be able to perform an external drag a set of files
  119. when the user drags outside of this container component.
  120. This method will be called when a drag operation moves outside the Juce-based window,
  121. and if you want it to then perform a file drag-and-drop, add the filenames you want
  122. to the array passed in, and return true.
  123. @param sourceDetails information about the source of the drag operation
  124. @param files on return, the filenames you want to drag
  125. @param canMoveFiles on return, true if it's ok for the receiver to move the files; false if
  126. it must make a copy of them (see the performExternalDragDropOfFiles() method)
  127. @see performExternalDragDropOfFiles
  128. */
  129. virtual bool shouldDropFilesWhenDraggedExternally (const DragAndDropTarget::SourceDetails& sourceDetails,
  130. StringArray& files, bool& canMoveFiles);
  131. /** Subclasses can override this to be told when a drag starts. */
  132. virtual void dragOperationStarted();
  133. /** Subclasses can override this to be told when a drag finishes. */
  134. virtual void dragOperationEnded();
  135. private:
  136. //==============================================================================
  137. class DragImageComponent;
  138. friend class DragImageComponent;
  139. friend struct ContainerDeletePolicy<DragImageComponent>;
  140. ScopedPointer<DragImageComponent> dragImageComponent;
  141. JUCE_DEPRECATED (virtual bool shouldDropFilesWhenDraggedExternally (const String&, Component*, StringArray&, bool&)) { return false; }
  142. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropContainer)
  143. };
  144. #endif // JUCE_DRAGANDDROPCONTAINER_H_INCLUDED