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.

247 lines
7.8KB

  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. const 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. {
  79. public:
  80. //==============================================================================
  81. DragAndDropDemoTarget()
  82. : message ("Drag-and-drop some rows from the top-left box onto this component!\n\n"
  83. "You can also drag-and-drop files here"),
  84. somethingIsBeingDraggedOver (false)
  85. {
  86. }
  87. ~DragAndDropDemoTarget()
  88. {
  89. }
  90. //==============================================================================
  91. void paint (Graphics& g)
  92. {
  93. g.fillAll (Colours::green.withAlpha (0.2f));
  94. // draw a red line around the comp if the user's currently dragging something over it..
  95. if (somethingIsBeingDraggedOver)
  96. {
  97. g.setColour (Colours::red);
  98. g.drawRect (0, 0, getWidth(), getHeight(), 3);
  99. }
  100. g.setColour (Colours::black);
  101. g.setFont (14.0f);
  102. g.drawFittedText (message, 10, 0, getWidth() - 20, getHeight(), Justification::centred, 4);
  103. }
  104. //==============================================================================
  105. // These methods implement the DragAndDropTarget interface, and allow our component
  106. // to accept drag-and-drop of objects from other Juce components..
  107. bool isInterestedInDragSource (const SourceDetails& /*dragSourceDetails*/)
  108. {
  109. // normally you'd check the sourceDescription value to see if it's the
  110. // sort of object that you're interested in before returning true, but for
  111. // the demo, we'll say yes to anything..
  112. return true;
  113. }
  114. void itemDragEnter (const SourceDetails& /*dragSourceDetails*/)
  115. {
  116. somethingIsBeingDraggedOver = true;
  117. repaint();
  118. }
  119. void itemDragMove (const SourceDetails& /*dragSourceDetails*/)
  120. {
  121. }
  122. void itemDragExit (const SourceDetails& /*dragSourceDetails*/)
  123. {
  124. somethingIsBeingDraggedOver = false;
  125. repaint();
  126. }
  127. void itemDropped (const SourceDetails& dragSourceDetails)
  128. {
  129. message = "last rows dropped: " + dragSourceDetails.description.toString();
  130. somethingIsBeingDraggedOver = false;
  131. repaint();
  132. }
  133. //==============================================================================
  134. // These methods implement the FileDragAndDropTarget interface, and allow our component
  135. // to accept drag-and-drop of files..
  136. bool isInterestedInFileDrag (const StringArray& /*files*/)
  137. {
  138. // normally you'd check these files to see if they're something that you're
  139. // interested in before returning true, but for the demo, we'll say yes to anything..
  140. return true;
  141. }
  142. void fileDragEnter (const StringArray& /*files*/, int /*x*/, int /*y*/)
  143. {
  144. somethingIsBeingDraggedOver = true;
  145. repaint();
  146. }
  147. void fileDragMove (const StringArray& /*files*/, int /*x*/, int /*y*/)
  148. {
  149. }
  150. void fileDragExit (const StringArray& /*files*/)
  151. {
  152. somethingIsBeingDraggedOver = false;
  153. repaint();
  154. }
  155. void filesDropped (const StringArray& files, int /*x*/, int /*y*/)
  156. {
  157. message = "files dropped: " + files.joinIntoString ("\n");
  158. somethingIsBeingDraggedOver = false;
  159. repaint();
  160. }
  161. private:
  162. String message;
  163. bool somethingIsBeingDraggedOver;
  164. };
  165. //==============================================================================
  166. class DragAndDropDemo : public Component,
  167. public DragAndDropContainer
  168. {
  169. public:
  170. //==============================================================================
  171. DragAndDropDemo()
  172. {
  173. setName ("Drag-and-Drop");
  174. addAndMakeVisible (&source);
  175. addAndMakeVisible (&target);
  176. }
  177. ~DragAndDropDemo()
  178. {
  179. }
  180. void resized()
  181. {
  182. source.setBounds (10, 10, 250, 150);
  183. target.setBounds (getWidth() - 260, getHeight() - 160, 250, 150);
  184. }
  185. private:
  186. DragAndDropDemoSource source;
  187. DragAndDropDemoTarget target;
  188. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragAndDropDemo);
  189. };
  190. //==============================================================================
  191. Component* createDragAndDropDemo()
  192. {
  193. return new DragAndDropDemo();
  194. }