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.

208 lines
10KB

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