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.

235 lines
7.5KB

  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 (T("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 (T("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) << T(" ");
  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 = T("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,
  117. Component* sourceComponent)
  118. {
  119. // normally you'd check the sourceDescription value to see if it's the
  120. // sort of object that you're interested in before returning true, but for
  121. // the demo, we'll say yes to anything..
  122. return true;
  123. }
  124. void itemDragEnter (const String& sourceDescription,
  125. Component* sourceComponent,
  126. int x, int y)
  127. {
  128. somethingIsBeingDraggedOver = true;
  129. repaint();
  130. }
  131. void itemDragMove (const String& sourceDescription,
  132. Component* sourceComponent,
  133. int x, int y)
  134. {
  135. }
  136. void itemDragExit (const String& sourceDescription,
  137. Component* sourceComponent)
  138. {
  139. somethingIsBeingDraggedOver = false;
  140. repaint();
  141. }
  142. void itemDropped (const String& sourceDescription,
  143. Component* sourceComponent,
  144. int x, int y)
  145. {
  146. message = T("last rows dropped: ") + sourceDescription;
  147. somethingIsBeingDraggedOver = false;
  148. repaint();
  149. }
  150. };
  151. //==============================================================================
  152. class DragAndDropDemo : public Component,
  153. public DragAndDropContainer
  154. {
  155. //==============================================================================
  156. DragAndDropDemoSource* source;
  157. DragAndDropDemoTarget* target;
  158. public:
  159. //==============================================================================
  160. DragAndDropDemo()
  161. {
  162. setName (T("Drag-and-Drop"));
  163. source = new DragAndDropDemoSource();
  164. addAndMakeVisible (source);
  165. target = new DragAndDropDemoTarget();
  166. addAndMakeVisible (target);
  167. }
  168. ~DragAndDropDemo()
  169. {
  170. deleteAllChildren();
  171. }
  172. void resized()
  173. {
  174. source->setBounds (10, 10, 250, 150);
  175. target->setBounds (getWidth() - 260, getHeight() - 160, 250, 150);
  176. }
  177. //==============================================================================
  178. // (need to put this in to disambiguate the new/delete operators used in the
  179. // two base classes).
  180. juce_UseDebuggingNewOperator
  181. };
  182. //==============================================================================
  183. Component* createDragAndDropDemo()
  184. {
  185. return new DragAndDropDemo();
  186. }