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.

276 lines
8.6KB

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