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.

268 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. FileSearchPathListComponent::FileSearchPathListComponent()
  16. : addButton ("+"),
  17. removeButton ("-"),
  18. changeButton (TRANS ("change...")),
  19. upButton ({}, DrawableButton::ImageOnButtonBackground),
  20. downButton ({}, DrawableButton::ImageOnButtonBackground)
  21. {
  22. listBox.setModel (this);
  23. addAndMakeVisible (listBox);
  24. listBox.setColour (ListBox::backgroundColourId, Colours::black.withAlpha (0.02f));
  25. listBox.setColour (ListBox::outlineColourId, Colours::black.withAlpha (0.1f));
  26. listBox.setOutlineThickness (1);
  27. addAndMakeVisible (addButton);
  28. addButton.onClick = [this] { addPath(); };
  29. addButton.setConnectedEdges (Button::ConnectedOnLeft | Button::ConnectedOnRight | Button::ConnectedOnBottom | Button::ConnectedOnTop);
  30. addAndMakeVisible (removeButton);
  31. removeButton.onClick = [this] { deleteSelected(); };
  32. removeButton.setConnectedEdges (Button::ConnectedOnLeft | Button::ConnectedOnRight | Button::ConnectedOnBottom | Button::ConnectedOnTop);
  33. addAndMakeVisible (changeButton);
  34. changeButton.onClick = [this] { editSelected(); };
  35. addAndMakeVisible (upButton);
  36. upButton.onClick = [this] { moveSelection (-1); };
  37. auto arrowColour = findColour (ListBox::textColourId);
  38. {
  39. Path arrowPath;
  40. arrowPath.addArrow ({ 50.0f, 100.0f, 50.0f, 0.0f }, 40.0f, 100.0f, 50.0f);
  41. DrawablePath arrowImage;
  42. arrowImage.setFill (arrowColour);
  43. arrowImage.setPath (arrowPath);
  44. upButton.setImages (&arrowImage);
  45. }
  46. addAndMakeVisible (downButton);
  47. downButton.onClick = [this] { moveSelection (1); };
  48. {
  49. Path arrowPath;
  50. arrowPath.addArrow ({ 50.0f, 0.0f, 50.0f, 100.0f }, 40.0f, 100.0f, 50.0f);
  51. DrawablePath arrowImage;
  52. arrowImage.setFill (arrowColour);
  53. arrowImage.setPath (arrowPath);
  54. downButton.setImages (&arrowImage);
  55. }
  56. updateButtons();
  57. }
  58. FileSearchPathListComponent::~FileSearchPathListComponent()
  59. {
  60. }
  61. void FileSearchPathListComponent::updateButtons()
  62. {
  63. const bool anythingSelected = listBox.getNumSelectedRows() > 0;
  64. removeButton.setEnabled (anythingSelected);
  65. changeButton.setEnabled (anythingSelected);
  66. upButton.setEnabled (anythingSelected);
  67. downButton.setEnabled (anythingSelected);
  68. }
  69. void FileSearchPathListComponent::changed()
  70. {
  71. listBox.updateContent();
  72. listBox.repaint();
  73. updateButtons();
  74. }
  75. //==============================================================================
  76. void FileSearchPathListComponent::setPath (const FileSearchPath& newPath)
  77. {
  78. if (newPath.toString() != path.toString())
  79. {
  80. path = newPath;
  81. changed();
  82. }
  83. }
  84. void FileSearchPathListComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
  85. {
  86. defaultBrowseTarget = newDefaultDirectory;
  87. }
  88. //==============================================================================
  89. int FileSearchPathListComponent::getNumRows()
  90. {
  91. return path.getNumPaths();
  92. }
  93. void FileSearchPathListComponent::paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected)
  94. {
  95. if (rowIsSelected)
  96. g.fillAll (findColour (TextEditor::highlightColourId));
  97. g.setColour (findColour (ListBox::textColourId));
  98. Font f (height * 0.7f);
  99. f.setHorizontalScale (0.9f);
  100. g.setFont (f);
  101. g.drawText (path[rowNumber].getFullPathName(),
  102. 4, 0, width - 6, height,
  103. Justification::centredLeft, true);
  104. }
  105. void FileSearchPathListComponent::deleteKeyPressed (int row)
  106. {
  107. if (isPositiveAndBelow (row, path.getNumPaths()))
  108. {
  109. path.remove (row);
  110. changed();
  111. }
  112. }
  113. void FileSearchPathListComponent::returnKeyPressed (int row)
  114. {
  115. #if JUCE_MODAL_LOOPS_PERMITTED
  116. FileChooser chooser (TRANS("Change folder..."), path[row], "*");
  117. if (chooser.browseForDirectory())
  118. {
  119. path.remove (row);
  120. path.add (chooser.getResult(), row);
  121. changed();
  122. }
  123. #else
  124. ignoreUnused (row);
  125. #endif
  126. }
  127. void FileSearchPathListComponent::listBoxItemDoubleClicked (int row, const MouseEvent&)
  128. {
  129. returnKeyPressed (row);
  130. }
  131. void FileSearchPathListComponent::selectedRowsChanged (int)
  132. {
  133. updateButtons();
  134. }
  135. void FileSearchPathListComponent::paint (Graphics& g)
  136. {
  137. g.fillAll (findColour (backgroundColourId));
  138. }
  139. void FileSearchPathListComponent::resized()
  140. {
  141. const int buttonH = 22;
  142. const int buttonY = getHeight() - buttonH - 4;
  143. listBox.setBounds (2, 2, getWidth() - 4, buttonY - 5);
  144. addButton.setBounds (2, buttonY, buttonH, buttonH);
  145. removeButton.setBounds (addButton.getRight(), buttonY, buttonH, buttonH);
  146. changeButton.changeWidthToFitText (buttonH);
  147. downButton.setSize (buttonH * 2, buttonH);
  148. upButton.setSize (buttonH * 2, buttonH);
  149. downButton.setTopRightPosition (getWidth() - 2, buttonY);
  150. upButton.setTopRightPosition (downButton.getX() - 4, buttonY);
  151. changeButton.setTopRightPosition (upButton.getX() - 8, buttonY);
  152. }
  153. bool FileSearchPathListComponent::isInterestedInFileDrag (const StringArray&)
  154. {
  155. return true;
  156. }
  157. void FileSearchPathListComponent::filesDropped (const StringArray& filenames, int, int mouseY)
  158. {
  159. for (int i = filenames.size(); --i >= 0;)
  160. {
  161. const File f (filenames[i]);
  162. if (f.isDirectory())
  163. {
  164. auto row = listBox.getRowContainingPosition (0, mouseY - listBox.getY());
  165. path.add (f, row);
  166. changed();
  167. }
  168. }
  169. }
  170. void FileSearchPathListComponent::addPath()
  171. {
  172. auto start = defaultBrowseTarget;
  173. if (start == File())
  174. start = path[0];
  175. if (start == File())
  176. start = File::getCurrentWorkingDirectory();
  177. #if JUCE_MODAL_LOOPS_PERMITTED
  178. FileChooser chooser (TRANS("Add a folder..."), start, "*");
  179. if (chooser.browseForDirectory())
  180. path.add (chooser.getResult(), listBox.getSelectedRow());
  181. changed();
  182. #else
  183. jassertfalse; // needs rewriting to deal with non-modal environments
  184. #endif
  185. }
  186. void FileSearchPathListComponent::deleteSelected()
  187. {
  188. deleteKeyPressed (listBox.getSelectedRow());
  189. changed();
  190. }
  191. void FileSearchPathListComponent::editSelected()
  192. {
  193. returnKeyPressed (listBox.getSelectedRow());
  194. changed();
  195. }
  196. void FileSearchPathListComponent::moveSelection (int delta)
  197. {
  198. jassert (delta == -1 || delta == 1);
  199. auto currentRow = listBox.getSelectedRow();
  200. if (isPositiveAndBelow (currentRow, path.getNumPaths()))
  201. {
  202. auto newRow = jlimit (0, path.getNumPaths() - 1, currentRow + delta);
  203. if (currentRow != newRow)
  204. {
  205. auto f = path[currentRow];
  206. path.remove (currentRow);
  207. path.add (f, newRow);
  208. listBox.selectRow (newRow);
  209. changed();
  210. }
  211. }
  212. }
  213. } // namespace juce