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.

195 lines
6.6KB

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