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.

227 lines
7.4KB

  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
  30. // its rows.
  31. setModel (this);
  32. setMultipleSelectionEnabled (true);
  33. }
  34. ~DragAndDropDemoSource()
  35. {
  36. }
  37. //==============================================================================
  38. // The following methods implement the necessary virtual functions from ListBoxModel,
  39. // telling the listbox how many rows there are, painting them, etc.
  40. int getNumRows()
  41. {
  42. return 30;
  43. }
  44. void paintListBoxItem (int rowNumber,
  45. Graphics& g,
  46. int width, int height,
  47. bool rowIsSelected)
  48. {
  49. if (rowIsSelected)
  50. g.fillAll (Colours::lightblue);
  51. g.setColour (Colours::black);
  52. g.setFont (height * 0.7f);
  53. g.drawText ("Row Number " + String (rowNumber + 1),
  54. 5, 0, width, height,
  55. Justification::centredLeft, true);
  56. }
  57. const String getDragSourceDescription (const SparseSet<int>& selectedRows)
  58. {
  59. // for our drag desctription, we'll just make a list of the selected
  60. // row numbers - this will be picked up by the drag target and displayed in
  61. // its box.
  62. String desc;
  63. for (int i = 0; i < selectedRows.size(); ++i)
  64. desc << (selectedRows [i] + 1) << " ";
  65. return desc.trim();
  66. }
  67. //==============================================================================
  68. // this just fills in the background of the listbox
  69. void paint (Graphics& g)
  70. {
  71. g.fillAll (Colours::white.withAlpha (0.7f));
  72. }
  73. /*void listBoxItemClicked (int row, const MouseEvent& e)
  74. {
  75. PopupMenu m;
  76. m.addItem (1, "sdfsdfs");
  77. m.show();
  78. //AlertWindow::showMessageBox (AlertWindow::InfoIcon, "asdfsadfads", "srdfsdfa");
  79. DocumentWindow* dw = new DocumentWindow ("sfdsd", Colours::white, DocumentWindow::allButtons, true);
  80. dw->setBounds (100, 100, 500, 500);
  81. dw->setVisible (true);
  82. }*/
  83. };
  84. //==============================================================================
  85. // and this is a component that can have things dropped onto it..
  86. class DragAndDropDemoTarget : public Component,
  87. public DragAndDropTarget
  88. {
  89. bool somethingIsBeingDraggedOver;
  90. String message;
  91. public:
  92. //==============================================================================
  93. DragAndDropDemoTarget()
  94. {
  95. somethingIsBeingDraggedOver = false;
  96. message = "Drag-and-drop some rows from the top-left box onto this component!";
  97. }
  98. ~DragAndDropDemoTarget()
  99. {
  100. }
  101. //==============================================================================
  102. void paint (Graphics& g)
  103. {
  104. g.fillAll (Colours::green.withAlpha (0.2f));
  105. // draw a red line around the comp if the user's currently dragging something over it..
  106. if (somethingIsBeingDraggedOver)
  107. {
  108. g.setColour (Colours::red);
  109. g.drawRect (0, 0, getWidth(), getHeight(), 3);
  110. }
  111. g.setColour (Colours::black);
  112. g.setFont (14.0f);
  113. g.drawFittedText (message, 10, 0, getWidth() - 20, getHeight(), Justification::centred, 4);
  114. }
  115. //==============================================================================
  116. bool isInterestedInDragSource (const String& /*sourceDescription*/, Component* /*sourceComponent*/)
  117. {
  118. // normally you'd check the sourceDescription value to see if it's the
  119. // sort of object that you're interested in before returning true, but for
  120. // the demo, we'll say yes to anything..
  121. return true;
  122. }
  123. void itemDragEnter (const String& /*sourceDescription*/, Component* /*sourceComponent*/, int /*x*/, int /*y*/)
  124. {
  125. somethingIsBeingDraggedOver = true;
  126. repaint();
  127. }
  128. void itemDragMove (const String& /*sourceDescription*/, Component* /*sourceComponent*/, int /*x*/, int /*y*/)
  129. {
  130. }
  131. void itemDragExit (const String& /*sourceDescription*/, Component* /*sourceComponent*/)
  132. {
  133. somethingIsBeingDraggedOver = false;
  134. repaint();
  135. }
  136. void itemDropped (const String& sourceDescription, Component* /*sourceComponent*/, int /*x*/, int /*y*/)
  137. {
  138. message = "last rows dropped: " + sourceDescription;
  139. somethingIsBeingDraggedOver = false;
  140. repaint();
  141. }
  142. };
  143. //==============================================================================
  144. class DragAndDropDemo : public Component,
  145. public DragAndDropContainer
  146. {
  147. //==============================================================================
  148. DragAndDropDemoSource* source;
  149. DragAndDropDemoTarget* target;
  150. public:
  151. //==============================================================================
  152. DragAndDropDemo()
  153. {
  154. setName ("Drag-and-Drop");
  155. source = new DragAndDropDemoSource();
  156. addAndMakeVisible (source);
  157. target = new DragAndDropDemoTarget();
  158. addAndMakeVisible (target);
  159. }
  160. ~DragAndDropDemo()
  161. {
  162. deleteAllChildren();
  163. }
  164. void resized()
  165. {
  166. source->setBounds (10, 10, 250, 150);
  167. target->setBounds (getWidth() - 260, getHeight() - 160, 250, 150);
  168. }
  169. //==============================================================================
  170. // (need to put this in to disambiguate the new/delete operators used in the
  171. // two base classes).
  172. juce_UseDebuggingNewOperator
  173. };
  174. //==============================================================================
  175. Component* createDragAndDropDemo()
  176. {
  177. return new DragAndDropDemo();
  178. }