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.

275 lines
8.4KB

  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. #include "../jucedemo_headers.h"
  18. //==============================================================================
  19. // this is the listbox containing the draggable source components..
  20. class DragAndDropDemoSource : public ListBox,
  21. public ListBoxModel
  22. {
  23. public:
  24. //==============================================================================
  25. DragAndDropDemoSource()
  26. : ListBox ("d+d source", 0)
  27. {
  28. // tells the ListBox that this object supplies the info about its rows.
  29. setModel (this);
  30. setMultipleSelectionEnabled (true);
  31. }
  32. //==============================================================================
  33. // The following methods implement the necessary virtual functions from ListBoxModel,
  34. // telling the listbox how many rows there are, painting them, etc.
  35. int getNumRows()
  36. {
  37. return 30;
  38. }
  39. void paintListBoxItem (int rowNumber,
  40. Graphics& g,
  41. int width, int height,
  42. bool rowIsSelected)
  43. {
  44. if (rowIsSelected)
  45. g.fillAll (Colours::lightblue);
  46. g.setColour (Colours::black);
  47. g.setFont (height * 0.7f);
  48. g.drawText ("Row Number " + String (rowNumber + 1),
  49. 5, 0, width, height,
  50. Justification::centredLeft, true);
  51. }
  52. var getDragSourceDescription (const SparseSet<int>& selectedRows)
  53. {
  54. // for our drag desctription, we'll just make a list of the selected
  55. // row numbers - this will be picked up by the drag target and displayed in
  56. // its box.
  57. String desc;
  58. for (int i = 0; i < selectedRows.size(); ++i)
  59. desc << (selectedRows [i] + 1) << " ";
  60. return desc.trim();
  61. }
  62. //==============================================================================
  63. // this just fills in the background of the listbox
  64. void paint (Graphics& g)
  65. {
  66. g.fillAll (Colours::white.withAlpha (0.7f));
  67. }
  68. };
  69. //==============================================================================
  70. // and this is a component that can have things dropped onto it..
  71. class DragAndDropDemoTarget : public Component,
  72. public DragAndDropTarget,
  73. public FileDragAndDropTarget,
  74. public TextDragAndDropTarget
  75. {
  76. public:
  77. //==============================================================================
  78. DragAndDropDemoTarget()
  79. : message ("Drag-and-drop some rows from the top-left box onto this component!\n\n"
  80. "You can also drag-and-drop files here"),
  81. somethingIsBeingDraggedOver (false)
  82. {
  83. }
  84. ~DragAndDropDemoTarget()
  85. {
  86. }
  87. //==============================================================================
  88. void paint (Graphics& g)
  89. {
  90. g.fillAll (Colours::green.withAlpha (0.2f));
  91. // draw a red line around the comp if the user's currently dragging something over it..
  92. if (somethingIsBeingDraggedOver)
  93. {
  94. g.setColour (Colours::red);
  95. g.drawRect (0, 0, getWidth(), getHeight(), 3);
  96. }
  97. g.setColour (Colours::black);
  98. g.setFont (14.0f);
  99. g.drawFittedText (message, getLocalBounds().reduced (10, 0), Justification::centred, 4);
  100. }
  101. //==============================================================================
  102. // These methods implement the DragAndDropTarget interface, and allow our component
  103. // to accept drag-and-drop of objects from other Juce components..
  104. bool isInterestedInDragSource (const SourceDetails& /*dragSourceDetails*/)
  105. {
  106. // normally you'd check the sourceDescription value to see if it's the
  107. // sort of object that you're interested in before returning true, but for
  108. // the demo, we'll say yes to anything..
  109. return true;
  110. }
  111. void itemDragEnter (const SourceDetails& /*dragSourceDetails*/)
  112. {
  113. somethingIsBeingDraggedOver = true;
  114. repaint();
  115. }
  116. void itemDragMove (const SourceDetails& /*dragSourceDetails*/)
  117. {
  118. }
  119. void itemDragExit (const SourceDetails& /*dragSourceDetails*/)
  120. {
  121. somethingIsBeingDraggedOver = false;
  122. repaint();
  123. }
  124. void itemDropped (const SourceDetails& dragSourceDetails)
  125. {
  126. message = "last rows dropped: " + dragSourceDetails.description.toString();
  127. somethingIsBeingDraggedOver = false;
  128. repaint();
  129. }
  130. //==============================================================================
  131. // These methods implement the FileDragAndDropTarget interface, and allow our component
  132. // to accept drag-and-drop of files..
  133. bool isInterestedInFileDrag (const StringArray& /*files*/)
  134. {
  135. // normally you'd check these files to see if they're something that you're
  136. // interested in before returning true, but for the demo, we'll say yes to anything..
  137. return true;
  138. }
  139. void fileDragEnter (const StringArray& /*files*/, int /*x*/, int /*y*/)
  140. {
  141. somethingIsBeingDraggedOver = true;
  142. repaint();
  143. }
  144. void fileDragMove (const StringArray& /*files*/, int /*x*/, int /*y*/)
  145. {
  146. }
  147. void fileDragExit (const StringArray& /*files*/)
  148. {
  149. somethingIsBeingDraggedOver = false;
  150. repaint();
  151. }
  152. void filesDropped (const StringArray& files, int /*x*/, int /*y*/)
  153. {
  154. message = "files dropped: " + files.joinIntoString ("\n");
  155. somethingIsBeingDraggedOver = false;
  156. repaint();
  157. }
  158. // These methods implement the TextDragAndDropTarget interface, and allow our component
  159. // to accept drag-and-drop of text..
  160. bool isInterestedInTextDrag (const String& /*text*/)
  161. {
  162. return true;
  163. }
  164. void textDragEnter (const String& /*text*/, int /*x*/, int /*y*/)
  165. {
  166. somethingIsBeingDraggedOver = true;
  167. repaint();
  168. }
  169. void textDragMove (const String& /*text*/, int /*x*/, int /*y*/)
  170. {
  171. }
  172. void textDragExit (const String& /*text*/)
  173. {
  174. somethingIsBeingDraggedOver = false;
  175. repaint();
  176. }
  177. void textDropped (const String& text, int /*x*/, int /*y*/)
  178. {
  179. message = "text dropped:\n" + text;
  180. somethingIsBeingDraggedOver = false;
  181. repaint();
  182. }
  183. private:
  184. String message;
  185. bool somethingIsBeingDraggedOver;
  186. };
  187. //==============================================================================
  188. class DragAndDropDemo : public Component,
  189. public DragAndDropContainer
  190. {
  191. public:
  192. //==============================================================================
  193. DragAndDropDemo()
  194. {
  195. setName ("Drag-and-Drop");
  196. addAndMakeVisible (&source);
  197. addAndMakeVisible (&target);
  198. }
  199. ~DragAndDropDemo()
  200. {
  201. }
  202. void resized()
  203. {
  204. source.setBounds (10, 10, 250, 150);
  205. target.setBounds (getWidth() - 260, getHeight() - 160, 250, 150);
  206. }
  207. private:
  208. DragAndDropDemoSource source;
  209. DragAndDropDemoTarget target;
  210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropDemo)
  211. };
  212. //==============================================================================
  213. Component* createDragAndDropDemo()
  214. {
  215. return new DragAndDropDemo();
  216. }