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.

210 lines
10KB

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