Audio plugin host https://kx.studio/carla
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.

235 lines
9.5KB

  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. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Listens for events happening to a FilenameComponent.
  23. Use FilenameComponent::addListener() and FilenameComponent::removeListener() to
  24. register one of these objects for event callbacks when the filename is changed.
  25. @see FilenameComponent
  26. @tags{GUI}
  27. */
  28. class JUCE_API FilenameComponentListener
  29. {
  30. public:
  31. /** Destructor. */
  32. virtual ~FilenameComponentListener() = default;
  33. /** This method is called after the FilenameComponent's file has been changed. */
  34. virtual void filenameComponentChanged (FilenameComponent* fileComponentThatHasChanged) = 0;
  35. };
  36. //==============================================================================
  37. /**
  38. Shows a filename as an editable text box, with a 'browse' button and a
  39. drop-down list for recently selected files.
  40. A handy component for dialogue boxes where you want the user to be able to
  41. select a file or directory.
  42. Attach an FilenameComponentListener using the addListener() method, and it will
  43. get called each time the user changes the filename, either by browsing for a file
  44. and clicking 'ok', or by typing a new filename into the box and pressing return.
  45. @see FileChooser, ComboBox
  46. @tags{GUI}
  47. */
  48. class JUCE_API FilenameComponent : public Component,
  49. public SettableTooltipClient,
  50. public FileDragAndDropTarget,
  51. private AsyncUpdater
  52. {
  53. public:
  54. //==============================================================================
  55. /** Creates a FilenameComponent.
  56. @param name the name for this component.
  57. @param currentFile the file to initially show in the box
  58. @param canEditFilename if true, the user can manually edit the filename; if false,
  59. they can only change it by browsing for a new file
  60. @param isDirectory if true, the file will be treated as a directory, and
  61. an appropriate directory browser used
  62. @param isForSaving if true, the file browser will allow non-existent files to
  63. be picked, as the file is assumed to be used for saving rather
  64. than loading
  65. @param fileBrowserWildcard a wildcard pattern to use in the file browser - e.g. "*.txt;*.foo".
  66. If an empty string is passed in, then the pattern is assumed to be "*"
  67. @param enforcedSuffix if this is non-empty, it is treated as a suffix that will be added
  68. to any filenames that are entered or chosen
  69. @param textWhenNothingSelected the message to display in the box before any filename is entered. (This
  70. will only appear if the initial file isn't valid)
  71. */
  72. FilenameComponent (const String& name,
  73. const File& currentFile,
  74. bool canEditFilename,
  75. bool isDirectory,
  76. bool isForSaving,
  77. const String& fileBrowserWildcard,
  78. const String& enforcedSuffix,
  79. const String& textWhenNothingSelected);
  80. /** Destructor. */
  81. ~FilenameComponent() override;
  82. //==============================================================================
  83. /** Returns the currently displayed filename. */
  84. File getCurrentFile() const;
  85. /** Returns the raw text that the user has entered. */
  86. String getCurrentFileText() const;
  87. /** Changes the current filename.
  88. @param newFile the new filename to use
  89. @param addToRecentlyUsedList if true, the filename will also be added to the
  90. drop-down list of recent files.
  91. @param notification whether to send a notification of the change to listeners.
  92. A notification will only be sent if the filename has changed.
  93. */
  94. void setCurrentFile (File newFile,
  95. bool addToRecentlyUsedList,
  96. NotificationType notification = sendNotificationAsync);
  97. /** Changes whether the use can type into the filename box.
  98. */
  99. void setFilenameIsEditable (bool shouldBeEditable);
  100. /** Sets a file or directory to be the default starting point for the browser to show.
  101. This is only used if the current file hasn't been set.
  102. */
  103. void setDefaultBrowseTarget (const File& newDefaultDirectory);
  104. /** This can be overridden to return a custom location that you want the dialog box
  105. to show when the browse button is pushed.
  106. The default implementation of this method will return either the current file
  107. (if one has been chosen) or the location that was set by setDefaultBrowseTarget().
  108. */
  109. virtual File getLocationToBrowse();
  110. /** Returns all the entries on the recent files list.
  111. This can be used in conjunction with setRecentlyUsedFilenames() for saving the
  112. state of this list.
  113. @see setRecentlyUsedFilenames
  114. */
  115. StringArray getRecentlyUsedFilenames() const;
  116. /** Sets all the entries on the recent files list.
  117. This can be used in conjunction with getRecentlyUsedFilenames() for saving the
  118. state of this list.
  119. @see getRecentlyUsedFilenames, addRecentlyUsedFile
  120. */
  121. void setRecentlyUsedFilenames (const StringArray& filenames);
  122. /** Adds an entry to the recently-used files dropdown list.
  123. If the file is already in the list, it will be moved to the top. A limit
  124. is also placed on the number of items that are kept in the list.
  125. @see getRecentlyUsedFilenames, setRecentlyUsedFilenames, setMaxNumberOfRecentFiles
  126. */
  127. void addRecentlyUsedFile (const File& file);
  128. /** Changes the limit for the number of files that will be stored in the recent-file list.
  129. */
  130. void setMaxNumberOfRecentFiles (int newMaximum);
  131. /** Changes the text shown on the 'browse' button.
  132. By default this button just says "..." but you can change it. The button itself
  133. can be changed using the look-and-feel classes, so it might not actually have any
  134. text on it.
  135. */
  136. void setBrowseButtonText (const String& browseButtonText);
  137. //==============================================================================
  138. /** Adds a listener that will be called when the selected file is changed. */
  139. void addListener (FilenameComponentListener* listener);
  140. /** Removes a previously-registered listener. */
  141. void removeListener (FilenameComponentListener* listener);
  142. /** Gives the component a tooltip. */
  143. void setTooltip (const String& newTooltip) override;
  144. //==============================================================================
  145. /** This abstract base class is implemented by LookAndFeel classes. */
  146. struct JUCE_API LookAndFeelMethods
  147. {
  148. virtual ~LookAndFeelMethods() = default;
  149. virtual Button* createFilenameComponentBrowseButton (const String& text) = 0;
  150. virtual void layoutFilenameComponent (FilenameComponent&, ComboBox* filenameBox, Button* browseButton) = 0;
  151. };
  152. //==============================================================================
  153. /** @internal */
  154. void paintOverChildren (Graphics&) override;
  155. /** @internal */
  156. void resized() override;
  157. /** @internal */
  158. void lookAndFeelChanged() override;
  159. /** @internal */
  160. bool isInterestedInFileDrag (const StringArray&) override;
  161. /** @internal */
  162. void filesDropped (const StringArray&, int, int) override;
  163. /** @internal */
  164. void fileDragEnter (const StringArray&, int, int) override;
  165. /** @internal */
  166. void fileDragExit (const StringArray&) override;
  167. /** @internal */
  168. KeyboardFocusTraverser* createFocusTraverser() override;
  169. private:
  170. //==============================================================================
  171. ComboBox filenameBox;
  172. String lastFilename;
  173. std::unique_ptr<Button> browseButton;
  174. int maxRecentFiles = 30;
  175. bool isDir, isSaving, isFileDragOver = false;
  176. String wildcard, enforcedSuffix, browseButtonText;
  177. ListenerList <FilenameComponentListener> listeners;
  178. File defaultBrowseFile;
  179. void showChooser();
  180. void handleAsyncUpdate() override;
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilenameComponent)
  182. };
  183. } // namespace juce