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

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