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.

265 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. void FileSearchPathListComponent::updateButtons()
  59. {
  60. const bool anythingSelected = listBox.getNumSelectedRows() > 0;
  61. removeButton.setEnabled (anythingSelected);
  62. changeButton.setEnabled (anythingSelected);
  63. upButton.setEnabled (anythingSelected);
  64. downButton.setEnabled (anythingSelected);
  65. }
  66. void FileSearchPathListComponent::changed()
  67. {
  68. listBox.updateContent();
  69. listBox.repaint();
  70. updateButtons();
  71. }
  72. //==============================================================================
  73. void FileSearchPathListComponent::setPath (const FileSearchPath& newPath)
  74. {
  75. if (newPath.toString() != path.toString())
  76. {
  77. path = newPath;
  78. changed();
  79. }
  80. }
  81. void FileSearchPathListComponent::setDefaultBrowseTarget (const File& newDefaultDirectory)
  82. {
  83. defaultBrowseTarget = newDefaultDirectory;
  84. }
  85. //==============================================================================
  86. int FileSearchPathListComponent::getNumRows()
  87. {
  88. return path.getNumPaths();
  89. }
  90. void FileSearchPathListComponent::paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected)
  91. {
  92. if (rowIsSelected)
  93. g.fillAll (findColour (TextEditor::highlightColourId));
  94. g.setColour (findColour (ListBox::textColourId));
  95. Font f ((float) height * 0.7f);
  96. f.setHorizontalScale (0.9f);
  97. g.setFont (f);
  98. g.drawText (path[rowNumber].getFullPathName(),
  99. 4, 0, width - 6, height,
  100. Justification::centredLeft, true);
  101. }
  102. void FileSearchPathListComponent::deleteKeyPressed (int row)
  103. {
  104. if (isPositiveAndBelow (row, path.getNumPaths()))
  105. {
  106. path.remove (row);
  107. changed();
  108. }
  109. }
  110. void FileSearchPathListComponent::returnKeyPressed (int row)
  111. {
  112. chooser = std::make_unique<FileChooser> (TRANS("Change folder..."), path[row], "*");
  113. auto chooserFlags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectDirectories;
  114. chooser->launchAsync (chooserFlags, [this, row] (const FileChooser& fc)
  115. {
  116. if (fc.getResult() == File{})
  117. return;
  118. path.remove (row);
  119. path.add (fc.getResult(), row);
  120. changed();
  121. });
  122. }
  123. void FileSearchPathListComponent::listBoxItemDoubleClicked (int row, const MouseEvent&)
  124. {
  125. returnKeyPressed (row);
  126. }
  127. void FileSearchPathListComponent::selectedRowsChanged (int)
  128. {
  129. updateButtons();
  130. }
  131. void FileSearchPathListComponent::paint (Graphics& g)
  132. {
  133. g.fillAll (findColour (backgroundColourId));
  134. }
  135. void FileSearchPathListComponent::resized()
  136. {
  137. const int buttonH = 22;
  138. const int buttonY = getHeight() - buttonH - 4;
  139. listBox.setBounds (2, 2, getWidth() - 4, buttonY - 5);
  140. addButton.setBounds (2, buttonY, buttonH, buttonH);
  141. removeButton.setBounds (addButton.getRight(), buttonY, buttonH, buttonH);
  142. changeButton.changeWidthToFitText (buttonH);
  143. downButton.setSize (buttonH * 2, buttonH);
  144. upButton.setSize (buttonH * 2, buttonH);
  145. downButton.setTopRightPosition (getWidth() - 2, buttonY);
  146. upButton.setTopRightPosition (downButton.getX() - 4, buttonY);
  147. changeButton.setTopRightPosition (upButton.getX() - 8, buttonY);
  148. }
  149. bool FileSearchPathListComponent::isInterestedInFileDrag (const StringArray&)
  150. {
  151. return true;
  152. }
  153. void FileSearchPathListComponent::filesDropped (const StringArray& filenames, int, int mouseY)
  154. {
  155. for (int i = filenames.size(); --i >= 0;)
  156. {
  157. const File f (filenames[i]);
  158. if (f.isDirectory())
  159. {
  160. auto row = listBox.getRowContainingPosition (0, mouseY - listBox.getY());
  161. path.add (f, row);
  162. changed();
  163. }
  164. }
  165. }
  166. void FileSearchPathListComponent::addPath()
  167. {
  168. auto start = defaultBrowseTarget;
  169. if (start == File())
  170. start = path[0];
  171. if (start == File())
  172. start = File::getCurrentWorkingDirectory();
  173. chooser = std::make_unique<FileChooser> (TRANS("Add a folder..."), start, "*");
  174. auto chooserFlags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectDirectories;
  175. chooser->launchAsync (chooserFlags, [this] (const FileChooser& fc)
  176. {
  177. if (fc.getResult() == File{})
  178. return;
  179. path.add (fc.getResult(), listBox.getSelectedRow());
  180. changed();
  181. });
  182. }
  183. void FileSearchPathListComponent::deleteSelected()
  184. {
  185. deleteKeyPressed (listBox.getSelectedRow());
  186. changed();
  187. }
  188. void FileSearchPathListComponent::editSelected()
  189. {
  190. returnKeyPressed (listBox.getSelectedRow());
  191. changed();
  192. }
  193. void FileSearchPathListComponent::moveSelection (int delta)
  194. {
  195. jassert (delta == -1 || delta == 1);
  196. auto currentRow = listBox.getSelectedRow();
  197. if (isPositiveAndBelow (currentRow, path.getNumPaths()))
  198. {
  199. auto newRow = jlimit (0, path.getNumPaths() - 1, currentRow + delta);
  200. if (currentRow != newRow)
  201. {
  202. auto f = path[currentRow];
  203. path.remove (currentRow);
  204. path.add (f, newRow);
  205. listBox.selectRow (newRow);
  206. changed();
  207. }
  208. }
  209. }
  210. } // namespace juce