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.

187 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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. private 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 (ValueWithDefault& valueToControl, const String& propertyName, bool isDir, bool thisOS = true,
  35. const String& wildcardsToUse = "*", const File& relativeRoot = File())
  36. : PropertyComponent (propertyName),
  37. text (valueToControl, propertyName, 1024, false),
  38. isDirectory (isDir), isThisOS (thisOS), wildcards (wildcardsToUse), root (relativeRoot)
  39. {
  40. textValue = valueToControl.getPropertyAsValue();
  41. init();
  42. }
  43. //==============================================================================
  44. void refresh() override {}
  45. void resized() override
  46. {
  47. auto bounds = getLocalBounds();
  48. text.setBounds (bounds.removeFromLeft (jmax (400, bounds.getWidth() - 55)));
  49. bounds.removeFromLeft (5);
  50. browseButton.setBounds (bounds);
  51. }
  52. void paintOverChildren (Graphics& g) override
  53. {
  54. if (highlightForDragAndDrop)
  55. {
  56. g.setColour (findColour (defaultHighlightColourId).withAlpha (0.5f));
  57. g.fillRect (getLookAndFeel().getPropertyComponentContentPosition (text));
  58. }
  59. }
  60. //==============================================================================
  61. bool isInterestedInFileDrag (const StringArray&) override { return true; }
  62. void fileDragEnter (const StringArray&, int, int) override { highlightForDragAndDrop = true; repaint(); }
  63. void fileDragExit (const StringArray&) override { highlightForDragAndDrop = false; repaint(); }
  64. void filesDropped (const StringArray& selectedFiles, int, int) override
  65. {
  66. setTo (selectedFiles[0]);
  67. highlightForDragAndDrop = false;
  68. repaint();
  69. }
  70. private:
  71. //==============================================================================
  72. void init()
  73. {
  74. textValue.addListener (this);
  75. text.setInterestedInFileDrag (false);
  76. addAndMakeVisible (text);
  77. browseButton.onClick = [this] { browse(); };
  78. addAndMakeVisible (browseButton);
  79. lookAndFeelChanged();
  80. }
  81. void setTo (File f)
  82. {
  83. if (isDirectory && ! f.isDirectory())
  84. f = f.getParentDirectory();
  85. auto pathName = (root == File()) ? f.getFullPathName()
  86. : f.getRelativePathFrom (root);
  87. text.setText (pathName);
  88. updateEditorColour();
  89. }
  90. void browse()
  91. {
  92. File currentFile = {};
  93. if (text.getText().isNotEmpty())
  94. currentFile = root.getChildFile (text.getText());
  95. if (isDirectory)
  96. {
  97. FileChooser chooser ("Select directory", currentFile);
  98. if (chooser.browseForDirectory())
  99. setTo (chooser.getResult());
  100. }
  101. else
  102. {
  103. FileChooser chooser ("Select file", currentFile, wildcards);
  104. if (chooser.browseForFileToOpen())
  105. setTo (chooser.getResult());
  106. }
  107. }
  108. void updateEditorColour()
  109. {
  110. if (isThisOS)
  111. {
  112. text.setColour (TextPropertyComponent::textColourId, findColour (widgetTextColourId));
  113. auto pathToCheck = text.getText();
  114. if (pathToCheck.isNotEmpty())
  115. {
  116. pathToCheck.replace ("${user.home}", "~");
  117. #if JUCE_WINDOWS
  118. if (pathToCheck.startsWith ("~"))
  119. pathToCheck = pathToCheck.replace ("~", File::getSpecialLocation (File::userHomeDirectory).getFullPathName());
  120. #endif
  121. if (! root.getChildFile (pathToCheck).exists())
  122. text.setColour (TextPropertyComponent::textColourId, Colours::red);
  123. }
  124. }
  125. }
  126. void valueChanged (Value&) override
  127. {
  128. updateEditorColour();
  129. }
  130. void lookAndFeelChanged() override
  131. {
  132. browseButton.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  133. browseButton.setColour (TextButton::textColourOffId, Colours::white);
  134. updateEditorColour();
  135. }
  136. //==============================================================================
  137. Value textValue;
  138. TextPropertyComponent text;
  139. TextButton browseButton { "..." };
  140. bool isDirectory, isThisOS, highlightForDragAndDrop = false;
  141. String wildcards;
  142. File root;
  143. //==============================================================================
  144. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilePathPropertyComponent)
  145. };