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.

juce_FilenameComponent.h 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. Listens for events happening to a FilenameComponent.
  24. Use FilenameComponent::addListener() and FilenameComponent::removeListener() to
  25. register one of these objects for event callbacks when the filename is changed.
  26. @see FilenameComponent
  27. */
  28. class JUCE_API FilenameComponentListener
  29. {
  30. public:
  31. /** Destructor. */
  32. virtual ~FilenameComponentListener() {}
  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. */
  47. class JUCE_API FilenameComponent : public Component,
  48. public SettableTooltipClient,
  49. public FileDragAndDropTarget,
  50. private AsyncUpdater,
  51. private Button::Listener,
  52. private ComboBox::Listener
  53. {
  54. public:
  55. //==============================================================================
  56. /** Creates a FilenameComponent.
  57. @param name the name for this component.
  58. @param currentFile the file to initially show in the box
  59. @param canEditFilename if true, the user can manually edit the filename; if false,
  60. they can only change it by browsing for a new file
  61. @param isDirectory if true, the file will be treated as a directory, and
  62. an appropriate directory browser used
  63. @param isForSaving if true, the file browser will allow non-existent files to
  64. be picked, as the file is assumed to be used for saving rather
  65. than loading
  66. @param fileBrowserWildcard a wildcard pattern to use in the file browser - e.g. "*.txt;*.foo".
  67. If an empty string is passed in, then the pattern is assumed to be "*"
  68. @param enforcedSuffix if this is non-empty, it is treated as a suffix that will be added
  69. to any filenames that are entered or chosen
  70. @param textWhenNothingSelected the message to display in the box before any filename is entered. (This
  71. will only appear if the initial file isn't valid)
  72. */
  73. FilenameComponent (const String& name,
  74. const File& currentFile,
  75. bool canEditFilename,
  76. bool isDirectory,
  77. bool isForSaving,
  78. const String& fileBrowserWildcard,
  79. const String& enforcedSuffix,
  80. const String& textWhenNothingSelected);
  81. /** Destructor. */
  82. ~FilenameComponent();
  83. //==============================================================================
  84. /** Returns the currently displayed filename. */
  85. File getCurrentFile() const;
  86. /** Returns the raw text that the user has entered. */
  87. String getCurrentFileText() const;
  88. /** Changes the current filename.
  89. @param newFile the new filename to use
  90. @param addToRecentlyUsedList if true, the filename will also be added to the
  91. drop-down list of recent files.
  92. @param notification whether to send a notification of the change to listeners
  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() {}
  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. ScopedPointer<Button> browseButton;
  174. int maxRecentFiles;
  175. bool isDir, isSaving, isFileDragOver;
  176. String wildcard, enforcedSuffix, browseButtonText;
  177. ListenerList <FilenameComponentListener> listeners;
  178. File defaultBrowseFile;
  179. void comboBoxChanged (ComboBox*) override;
  180. void buttonClicked (Button*) override;
  181. void handleAsyncUpdate() override;
  182. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilenameComponent)
  183. };
  184. } // namespace juce