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.

194 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. /** A PropertyComponent for selecting files or folders.
  21. The user may drag files over the property box, enter the path manually and/or click
  22. the '...' button to open a file selection dialog box.
  23. */
  24. class FilePathPropertyComponent : public PropertyComponent,
  25. public FileDragAndDropTarget,
  26. private Value::Listener
  27. {
  28. public:
  29. FilePathPropertyComponent (Value valueToControl, const String& propertyName, bool isDir, bool thisOS = true,
  30. const String& wildcardsToUse = "*", const File& relativeRoot = File())
  31. : PropertyComponent (propertyName),
  32. text (valueToControl, propertyName, 1024, false),
  33. isDirectory (isDir), isThisOS (thisOS), wildcards (wildcardsToUse), root (relativeRoot)
  34. {
  35. textValue.referTo (valueToControl);
  36. init();
  37. }
  38. /** Displays a default value when no value is specified by the user. */
  39. FilePathPropertyComponent (ValueWithDefault& valueToControl, const String& propertyName, bool isDir, bool thisOS = true,
  40. const String& wildcardsToUse = "*", const File& relativeRoot = File())
  41. : PropertyComponent (propertyName),
  42. text (valueToControl, propertyName, 1024, false),
  43. isDirectory (isDir), isThisOS (thisOS), wildcards (wildcardsToUse), root (relativeRoot)
  44. {
  45. textValue = valueToControl.getPropertyAsValue();
  46. init();
  47. }
  48. //==============================================================================
  49. void refresh() override {}
  50. void resized() override
  51. {
  52. auto bounds = getLocalBounds();
  53. text.setBounds (bounds.removeFromLeft (jmax (400, bounds.getWidth() - 55)));
  54. bounds.removeFromLeft (5);
  55. browseButton.setBounds (bounds);
  56. }
  57. void paintOverChildren (Graphics& g) override
  58. {
  59. if (highlightForDragAndDrop)
  60. {
  61. g.setColour (findColour (defaultHighlightColourId).withAlpha (0.5f));
  62. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (text));
  63. }
  64. }
  65. //==============================================================================
  66. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  67. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  68. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  69. void filesDropped (const StringArray& selectedFiles, int, int) override
  70. {
  71. setTo (selectedFiles[0]);
  72. highlightForDragAndDrop = false;
  73. repaint();
  74. }
  75. private:
  76. //==============================================================================
  77. void init()
  78. {
  79. textValue.addListener (this);
  80. text.setInterestedInFileDrag (false);
  81. addAndMakeVisible (text);
  82. browseButton.onClick = [this] { browse(); };
  83. addAndMakeVisible (browseButton);
  84. lookAndFeelChanged();
  85. }
  86. void setTo (File f)
  87. {
  88. if (isDirectory && ! f.isDirectory())
  89. f = f.getParentDirectory();
  90. auto pathName = (root == File()) ? f.getFullPathName()
  91. : f.getRelativePathFrom (root);
  92. text.setText (pathName);
  93. updateEditorColour();
  94. }
  95. void browse()
  96. {
  97. File currentFile = {};
  98. if (text.getText().isNotEmpty())
  99. currentFile = root.getChildFile (text.getText());
  100. if (isDirectory)
  101. {
  102. FileChooser chooser ("Select directory", currentFile);
  103. if (chooser.browseForDirectory())
  104. setTo (chooser.getResult());
  105. }
  106. else
  107. {
  108. FileChooser chooser ("Select file", currentFile, wildcards);
  109. if (chooser.browseForFileToOpen())
  110. setTo (chooser.getResult());
  111. }
  112. }
  113. void updateEditorColour()
  114. {
  115. if (isThisOS)
  116. {
  117. text.setColour (TextPropertyComponent::textColourId, findColour (widgetTextColourId));
  118. auto pathToCheck = text.getText();
  119. if (pathToCheck.isNotEmpty())
  120. {
  121. pathToCheck.replace ("${user.home}", "~");
  122. #if JUCE_WINDOWS
  123. if (pathToCheck.startsWith ("~"))
  124. pathToCheck = pathToCheck.replace ("~", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  125. #endif
  126. if (! root.getChildFile (pathToCheck).exists())
  127. text.setColour (TextPropertyComponent::textColourId, Colours::red);
  128. }
  129. }
  130. }
  131. void valueChanged (Value&) override
  132. {
  133. updateEditorColour();
  134. }
  135. void lookAndFeelChanged() override
  136. {
  137. browseButton.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  138. browseButton.setColour (TextButton::textColourOffId, Colours::white);
  139. updateEditorColour();
  140. }
  141. //==============================================================================
  142. Value textValue;
  143. TextPropertyComponent text;
  144. TextButton browseButton { "..." };
  145. bool isDirectory, isThisOS, highlightForDragAndDrop = false;
  146. String wildcards;
  147. File root;
  148. //==============================================================================
  149. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  150. };