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.

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