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.

184 lines
8.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_DRAGANDDROPCONTAINER_JUCEHEADER__
  19. #define __JUCE_DRAGANDDROPCONTAINER_JUCEHEADER__
  20. #include "juce_DragAndDropTarget.h"
  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 Component::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. const Image& dragImage = Image::null,
  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 an empty string, otherwise it's the
  78. string that was passed into startDragging().
  79. @see startDragging
  80. */
  81. String getCurrentDragDescription() const;
  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 0 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. private:
  132. //==============================================================================
  133. friend class DragImageComponent;
  134. ScopedPointer <Component> dragImageComponent;
  135. String currentDragDesc;
  136. JUCE_DEPRECATED (virtual bool shouldDropFilesWhenDraggedExternally (const String&, Component*, StringArray&, bool&)) { return false; }
  137. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropContainer);
  138. };
  139. #endif // __JUCE_DRAGANDDROPCONTAINER_JUCEHEADER__