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.

218 lines
8.0KB

  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. {
  45. InnerComponent (Value v, bool isDir, const String& wc, const File& rt, const bool multiplePaths)
  46. : value (v),
  47. isDirectory (isDir),
  48. highlightForDragAndDrop (false),
  49. wildcards (wc),
  50. root (rt),
  51. button ("..."),
  52. supportsMultiplePaths (multiplePaths)
  53. {
  54. addAndMakeVisible (textbox);
  55. textbox.getTextValue().referTo (value);
  56. textbox.onReturnKey = [this] { updateEditorColour (textbox); };
  57. textbox.onFocusLost = [this] { updateEditorColour (textbox); };
  58. addAndMakeVisible (button);
  59. button.onClick = [this] { browse(); };
  60. lookAndFeelChanged();
  61. }
  62. void paintOverChildren (Graphics& g) override
  63. {
  64. if (highlightForDragAndDrop)
  65. {
  66. g.setColour (findColour (defaultHighlightColourId).withAlpha (0.5f));
  67. g.fillRect (textbox.getBounds());
  68. }
  69. }
  70. void resized() override
  71. {
  72. juce::Rectangle<int> r (getLocalBounds());
  73. button.setBounds (r.removeFromRight (30));
  74. textbox.setBounds (r);
  75. }
  76. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  77. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  78. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  79. void filesDropped (const StringArray& files, int, int) override
  80. {
  81. const File firstFile (files[0]);
  82. if (isDirectory)
  83. setTo (firstFile.isDirectory() ? firstFile
  84. : firstFile.getParentDirectory());
  85. else
  86. setTo (firstFile);
  87. highlightForDragAndDrop = false;
  88. repaint();
  89. }
  90. void browse()
  91. {
  92. auto currentFile = root.getChildFile (value.toString());
  93. if (isDirectory)
  94. {
  95. FileChooser chooser ("Select directory", currentFile);
  96. if (chooser.browseForDirectory())
  97. setTo (chooser.getResult());
  98. }
  99. else
  100. {
  101. FileChooser chooser ("Select file", currentFile, wildcards);
  102. if (chooser.browseForFileToOpen())
  103. setTo (chooser.getResult());
  104. }
  105. }
  106. void updateEditorColour (TextEditor& editor)
  107. {
  108. if (supportsMultiplePaths)
  109. {
  110. auto paths = StringArray::fromTokens (editor.getTextValue().toString(), ";", {});
  111. editor.clear();
  112. AttributedString str;
  113. for (auto p : paths)
  114. {
  115. if (root.getChildFile (p.trim()).exists()) editor.setColour (TextEditor::textColourId, findColour (widgetTextColourId));
  116. else editor.setColour (TextEditor::textColourId, Colours::red);
  117. editor.insertTextAtCaret (p);
  118. if (paths.indexOf (p) < paths.size() - 1)
  119. {
  120. editor.setColour (TextEditor::textColourId, findColour (widgetTextColourId));
  121. editor.insertTextAtCaret (";");
  122. }
  123. }
  124. editor.setColour (TextEditor::textColourId, findColour (widgetTextColourId));
  125. }
  126. else
  127. {
  128. auto pathToCheck = editor.getTextValue().toString();
  129. //android SDK/NDK paths
  130. if (pathToCheck.contains ("${user.home}"))
  131. pathToCheck = pathToCheck.replace ("${user.home}", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  132. #if JUCE_WINDOWS
  133. if (pathToCheck.startsWith ("~"))
  134. pathToCheck = pathToCheck.replace ("~", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  135. #endif
  136. const auto currentFile = root.getChildFile (pathToCheck);
  137. if (currentFile.exists())
  138. editor.applyColourToAllText (findColour (widgetTextColourId));
  139. else
  140. editor.applyColourToAllText (Colours::red);
  141. }
  142. }
  143. void setTo (const File& f)
  144. {
  145. auto pathName = (root == File()) ? f.getFullPathName()
  146. : f.getRelativePathFrom (root);
  147. if (supportsMultiplePaths && value.toString().isNotEmpty())
  148. value = value.toString().trimCharactersAtEnd (" ;") + "; " + pathName;
  149. else
  150. value = pathName;
  151. updateEditorColour (textbox);
  152. }
  153. void lookAndFeelChanged() override
  154. {
  155. textbox.setColour (TextEditor::backgroundColourId, findColour (widgetBackgroundColourId));
  156. textbox.setColour (TextEditor::outlineColourId, Colours::transparentBlack);
  157. updateEditorColour (textbox);
  158. button.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  159. button.setColour (TextButton::textColourOffId, Colours::white);
  160. }
  161. Value value;
  162. bool isDirectory, highlightForDragAndDrop;
  163. String wildcards;
  164. File root;
  165. TextEditor textbox;
  166. TextButton button;
  167. bool supportsMultiplePaths;
  168. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InnerComponent)
  169. };
  170. InnerComponent innerComp; // Used so that the PropertyComponent auto first-child positioning works
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  172. };