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.

146 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCER_FILEPATHPROPERTYCOMPONENT_H_INCLUDED
  18. #define JUCER_FILEPATHPROPERTYCOMPONENT_H_INCLUDED
  19. class FilePathPropertyComponent : public PropertyComponent
  20. {
  21. public:
  22. /** A Property Component for selecting files or folders.
  23. The user may drag files over the property box, enter the path
  24. manually and/or click the '...' button to open a file selection
  25. dialog box
  26. */
  27. FilePathPropertyComponent (Value valueToControl,
  28. const String& propertyDescription,
  29. bool isDirectory,
  30. const String& wildcards = "*",
  31. const File& rootToUseForRelativePaths = File())
  32. : PropertyComponent (propertyDescription),
  33. innerComp (valueToControl, isDirectory, wildcards, rootToUseForRelativePaths)
  34. {
  35. addAndMakeVisible (innerComp);
  36. }
  37. void refresh() override {} // N/A
  38. private:
  39. struct InnerComponent : public Component,
  40. public FileDragAndDropTarget,
  41. private Button::Listener
  42. {
  43. InnerComponent (Value v, bool isDir, const String& wc, const File& rt)
  44. : value (v),
  45. isDirectory (isDir),
  46. highlightForDragAndDrop (false),
  47. wildcards (wc),
  48. root (rt),
  49. button ("...")
  50. {
  51. addAndMakeVisible (textbox);
  52. textbox.getTextValue().referTo (value);
  53. addAndMakeVisible (button);
  54. button.addListener (this);
  55. }
  56. void paintOverChildren (Graphics& g) override
  57. {
  58. if (highlightForDragAndDrop)
  59. {
  60. g.setColour (Colours::green.withAlpha (0.1f));
  61. g.fillRect (getLocalBounds());
  62. }
  63. }
  64. void resized() override
  65. {
  66. juce::Rectangle<int> r (getLocalBounds());
  67. button.setBounds (r.removeFromRight (24));
  68. textbox.setBounds (r);
  69. }
  70. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  71. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  72. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  73. void filesDropped (const StringArray& files, int, int) override
  74. {
  75. const File firstFile (files[0]);
  76. if (isDirectory)
  77. setTo (firstFile.isDirectory() ? firstFile
  78. : firstFile.getParentDirectory());
  79. else
  80. setTo (firstFile);
  81. }
  82. void buttonClicked (Button*) override
  83. {
  84. const File currentFile (root.getChildFile (value.toString()));
  85. if (isDirectory)
  86. {
  87. FileChooser chooser ("Select directory", currentFile);
  88. if (chooser.browseForDirectory())
  89. setTo (chooser.getResult());
  90. }
  91. else
  92. {
  93. FileChooser chooser ("Select file", currentFile, wildcards);
  94. if (chooser.browseForFileToOpen())
  95. setTo (chooser.getResult());
  96. }
  97. }
  98. void setTo (const File& f)
  99. {
  100. value = (root == File()) ? f.getFullPathName()
  101. : f.getRelativePathFrom (root);
  102. }
  103. Value value;
  104. bool isDirectory, highlightForDragAndDrop;
  105. String wildcards;
  106. File root;
  107. TextEditor textbox;
  108. TextButton button;
  109. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InnerComponent)
  110. };
  111. InnerComponent innerComp; // Used so that the PropertyComponent auto first-child positioning works
  112. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  113. };
  114. #endif // JUCER_FILEPATHPROPERTYCOMPONENT_H_INCLUDED