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.

222 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. //==============================================================================
  21. class FilePathPropertyComponent : public PropertyComponent
  22. {
  23. public:
  24. /** A Property Component for selecting files or folders.
  25. The user may drag files over the property box, enter the path
  26. manually and/or click the '...' button to open a file selection
  27. dialog box
  28. */
  29. FilePathPropertyComponent (Value valueToControl,
  30. const String& propertyDescription,
  31. bool isDirectory,
  32. const String& wildcards = "*",
  33. const File& rootToUseForRelativePaths = File(),
  34. const bool supportsMultiplePaths = false)
  35. : PropertyComponent (propertyDescription),
  36. innerComp (valueToControl, isDirectory, wildcards, rootToUseForRelativePaths, supportsMultiplePaths)
  37. {
  38. addAndMakeVisible (innerComp);
  39. }
  40. void refresh() override {} // N/A
  41. private:
  42. struct InnerComponent : public Component,
  43. public FileDragAndDropTarget,
  44. private Button::Listener,
  45. private TextEditor::Listener
  46. {
  47. InnerComponent (Value v, bool isDir, const String& wc, const File& rt, const bool multiplePaths)
  48. : value (v),
  49. isDirectory (isDir),
  50. highlightForDragAndDrop (false),
  51. wildcards (wc),
  52. root (rt),
  53. button ("..."),
  54. supportsMultiplePaths (multiplePaths)
  55. {
  56. addAndMakeVisible (textbox);
  57. textbox.getTextValue().referTo (value);
  58. textbox.addListener (this);
  59. addAndMakeVisible (button);
  60. button.addListener (this);
  61. lookAndFeelChanged();
  62. }
  63. void paintOverChildren (Graphics& g) override
  64. {
  65. if (highlightForDragAndDrop)
  66. {
  67. g.setColour (findColour (defaultHighlightColourId).withAlpha (0.5f));
  68. g.fillRect (textbox.getBounds());
  69. }
  70. }
  71. void resized() override
  72. {
  73. juce::Rectangle<int> r (getLocalBounds());
  74. button.setBounds (r.removeFromRight (30));
  75. textbox.setBounds (r);
  76. }
  77. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  78. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  79. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  80. void filesDropped (const StringArray& files, int, int) override
  81. {
  82. const File firstFile (files[0]);
  83. if (isDirectory)
  84. setTo (firstFile.isDirectory() ? firstFile
  85. : firstFile.getParentDirectory());
  86. else
  87. setTo (firstFile);
  88. highlightForDragAndDrop = false;
  89. repaint();
  90. }
  91. void buttonClicked (Button*) override
  92. {
  93. const File currentFile (root.getChildFile (value.toString()));
  94. if (isDirectory)
  95. {
  96. FileChooser chooser ("Select directory", currentFile);
  97. if (chooser.browseForDirectory())
  98. setTo (chooser.getResult());
  99. }
  100. else
  101. {
  102. FileChooser chooser ("Select file", currentFile, wildcards);
  103. if (chooser.browseForFileToOpen())
  104. setTo (chooser.getResult());
  105. }
  106. }
  107. void textEditorReturnKeyPressed (TextEditor& editor) override { updateEditorColour (editor); }
  108. void textEditorFocusLost (TextEditor& editor) override { updateEditorColour (editor); }
  109. void updateEditorColour (TextEditor& editor)
  110. {
  111. if (supportsMultiplePaths)
  112. {
  113. auto paths = StringArray::fromTokens (editor.getTextValue().toString(), ";", {});
  114. editor.clear();
  115. AttributedString str;
  116. for (auto p : paths)
  117. {
  118. if (root.getChildFile (p.trim()).exists()) editor.setColour (TextEditor::textColourId, findColour (widgetTextColourId));
  119. else editor.setColour (TextEditor::textColourId, Colours::red);
  120. editor.insertTextAtCaret (p);
  121. if (paths.indexOf (p) < paths.size() - 1)
  122. {
  123. editor.setColour (TextEditor::textColourId, findColour (widgetTextColourId));
  124. editor.insertTextAtCaret (";");
  125. }
  126. }
  127. editor.setColour (TextEditor::textColourId, findColour (widgetTextColourId));
  128. }
  129. else
  130. {
  131. auto pathToCheck = editor.getTextValue().toString();
  132. //android SDK/NDK paths
  133. if (pathToCheck.contains ("${user.home}"))
  134. pathToCheck = pathToCheck.replace ("${user.home}", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  135. #if JUCE_WINDOWS
  136. if (pathToCheck.startsWith ("~"))
  137. pathToCheck = pathToCheck.replace ("~", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  138. #endif
  139. const auto currentFile = root.getChildFile (pathToCheck);
  140. if (currentFile.exists())
  141. editor.applyColourToAllText (findColour (widgetTextColourId));
  142. else
  143. editor.applyColourToAllText (Colours::red);
  144. }
  145. }
  146. void setTo (const File& f)
  147. {
  148. auto pathName = (root == File()) ? f.getFullPathName()
  149. : f.getRelativePathFrom (root);
  150. if (supportsMultiplePaths && value.toString().isNotEmpty())
  151. value = value.toString().trimCharactersAtEnd (" ;") + "; " + pathName;
  152. else
  153. value = pathName;
  154. updateEditorColour (textbox);
  155. }
  156. void lookAndFeelChanged() override
  157. {
  158. textbox.setColour (TextEditor::backgroundColourId, findColour (widgetBackgroundColourId));
  159. textbox.setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  160. updateEditorColour (textbox);
  161. button.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  162. button.setColour (TextButton::textColourOffId, Colours::white);
  163. }
  164. Value value;
  165. bool isDirectory, highlightForDragAndDrop;
  166. String wildcards;
  167. File root;
  168. TextEditor textbox;
  169. TextButton button;
  170. bool supportsMultiplePaths;
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InnerComponent)
  172. };
  173. InnerComponent innerComp; // Used so that the PropertyComponent auto first-child positioning works
  174. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  175. };