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.

243 lines
8.5KB

  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. #pragma once
  14. //==============================================================================
  15. /** A PropertyComponent for selecting files or folders.
  16. The user may drag files over the property box, enter the path manually and/or click
  17. the '...' button to open a file selection dialog box.
  18. */
  19. class FilePathPropertyComponent : public PropertyComponent,
  20. public FileDragAndDropTarget,
  21. protected Value::Listener
  22. {
  23. public:
  24. FilePathPropertyComponent (Value valueToControl, const String& propertyName, bool isDir, bool thisOS = true,
  25. const String& wildcardsToUse = "*", const File& relativeRoot = File())
  26. : PropertyComponent (propertyName),
  27. text (valueToControl, propertyName, 1024, false),
  28. isDirectory (isDir), isThisOS (thisOS), wildcards (wildcardsToUse), root (relativeRoot)
  29. {
  30. textValue.referTo (valueToControl);
  31. init();
  32. }
  33. /** Displays a default value when no value is specified by the user. */
  34. FilePathPropertyComponent (ValueTreePropertyWithDefault valueToControl,
  35. const String& propertyName,
  36. bool isDir,
  37. bool thisOS = true,
  38. const String& wildcardsToUse = "*",
  39. const File& relativeRoot = File())
  40. : PropertyComponent (propertyName),
  41. text (valueToControl, propertyName, 1024, false),
  42. isDirectory (isDir), isThisOS (thisOS), wildcards (wildcardsToUse), root (relativeRoot)
  43. {
  44. textValue = valueToControl.getPropertyAsValue();
  45. init();
  46. }
  47. //==============================================================================
  48. void refresh() override {}
  49. void resized() override
  50. {
  51. auto bounds = getLocalBounds();
  52. text.setBounds (bounds.removeFromLeft (jmax (400, bounds.getWidth() - 55)));
  53. bounds.removeFromLeft (5);
  54. browseButton.setBounds (bounds);
  55. }
  56. void paintOverChildren (Graphics& g) override
  57. {
  58. if (highlightForDragAndDrop)
  59. {
  60. g.setColour (findColour (defaultHighlightColourId).withAlpha (0.5f));
  61. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (text));
  62. }
  63. }
  64. //==============================================================================
  65. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  66. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  67. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  68. void filesDropped (const StringArray& selectedFiles, int, int) override
  69. {
  70. setTo (selectedFiles[0]);
  71. highlightForDragAndDrop = false;
  72. repaint();
  73. }
  74. protected:
  75. void valueChanged (Value&) override
  76. {
  77. updateEditorColour();
  78. }
  79. private:
  80. //==============================================================================
  81. void init()
  82. {
  83. textValue.addListener (this);
  84. text.setInterestedInFileDrag (false);
  85. addAndMakeVisible (text);
  86. browseButton.onClick = [this] { browse(); };
  87. addAndMakeVisible (browseButton);
  88. lookAndFeelChanged();
  89. }
  90. void setTo (File f)
  91. {
  92. if (isDirectory && ! f.isDirectory())
  93. f = f.getParentDirectory();
  94. auto pathName = (root == File()) ? f.getFullPathName()
  95. : f.getRelativePathFrom (root);
  96. text.setText (pathName);
  97. updateEditorColour();
  98. }
  99. void browse()
  100. {
  101. File currentFile = {};
  102. if (text.getText().isNotEmpty())
  103. currentFile = root.getChildFile (text.getText());
  104. if (isDirectory)
  105. {
  106. chooser = std::make_unique<FileChooser> ("Select directory", currentFile);
  107. auto chooserFlags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectDirectories;
  108. chooser->launchAsync (chooserFlags, [this] (const FileChooser& fc)
  109. {
  110. if (fc.getResult() == File{})
  111. return;
  112. setTo (fc.getResult());
  113. });
  114. }
  115. else
  116. {
  117. chooser = std::make_unique<FileChooser> ("Select file", currentFile, wildcards);
  118. auto chooserFlags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles;
  119. chooser->launchAsync (chooserFlags, [this] (const FileChooser& fc)
  120. {
  121. if (fc.getResult() == File{})
  122. return;
  123. setTo (fc.getResult());
  124. });
  125. }
  126. }
  127. void updateEditorColour()
  128. {
  129. if (isThisOS)
  130. {
  131. text.setColour (TextPropertyComponent::textColourId, findColour (widgetTextColourId));
  132. auto pathToCheck = text.getText();
  133. if (pathToCheck.isNotEmpty())
  134. {
  135. pathToCheck.replace ("${user.home}", "~");
  136. #if JUCE_WINDOWS
  137. if (pathToCheck.startsWith ("~"))
  138. pathToCheck = pathToCheck.replace ("~", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  139. #endif
  140. if (! root.getChildFile (pathToCheck).exists())
  141. text.setColour (TextPropertyComponent::textColourId, Colours::red);
  142. }
  143. }
  144. }
  145. void lookAndFeelChanged() override
  146. {
  147. browseButton.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  148. browseButton.setColour (TextButton::textColourOffId, Colours::white);
  149. updateEditorColour();
  150. }
  151. //==============================================================================
  152. Value textValue;
  153. TextPropertyComponent text;
  154. TextButton browseButton { "..." };
  155. bool isDirectory, isThisOS, highlightForDragAndDrop = false;
  156. String wildcards;
  157. File root;
  158. std::unique_ptr<FileChooser> chooser;
  159. //==============================================================================
  160. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  161. };
  162. //==============================================================================
  163. class FilePathPropertyComponentWithEnablement : public FilePathPropertyComponent
  164. {
  165. public:
  166. FilePathPropertyComponentWithEnablement (const ValueTreePropertyWithDefault& valueToControl,
  167. ValueTreePropertyWithDefault valueToListenTo,
  168. const String& propertyName,
  169. bool isDir,
  170. bool thisOS = true,
  171. const String& wildcardsToUse = "*",
  172. const File& relativeRoot = File())
  173. : FilePathPropertyComponent (valueToControl,
  174. propertyName,
  175. isDir,
  176. thisOS,
  177. wildcardsToUse,
  178. relativeRoot),
  179. propertyWithDefault (valueToListenTo),
  180. value (valueToListenTo.getPropertyAsValue())
  181. {
  182. value.addListener (this);
  183. valueChanged (value);
  184. }
  185. ~FilePathPropertyComponentWithEnablement() override { value.removeListener (this); }
  186. private:
  187. void valueChanged (Value& v) override
  188. {
  189. FilePathPropertyComponent::valueChanged (v);
  190. setEnabled (propertyWithDefault.get());
  191. }
  192. ValueTreePropertyWithDefault propertyWithDefault;
  193. Value value;
  194. };