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

10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #pragma once
  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. */
  27. class JUCE_API FilenameComponentListener
  28. {
  29. public:
  30. /** Destructor. */
  31. virtual ~FilenameComponentListener() {}
  32. /** This method is called after the FilenameComponent's file has been changed. */
  33. virtual void filenameComponentChanged (FilenameComponent* fileComponentThatHasChanged) = 0;
  34. };
  35. //==============================================================================
  36. /**
  37. Shows a filename as an editable text box, with a 'browse' button and a
  38. drop-down list for recently selected files.
  39. A handy component for dialogue boxes where you want the user to be able to
  40. select a file or directory.
  41. Attach an FilenameComponentListener using the addListener() method, and it will
  42. get called each time the user changes the filename, either by browsing for a file
  43. and clicking 'ok', or by typing a new filename into the box and pressing return.
  44. @see FileChooser, ComboBox
  45. */
  46. class JUCE_API FilenameComponent : public Component,
  47. public SettableTooltipClient,
  48. public FileDragAndDropTarget,
  49. private AsyncUpdater,
  50. private Button::Listener,
  51. private ComboBox::Listener
  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();
  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. */
  93. void setCurrentFile (File newFile,
  94. bool addToRecentlyUsedList,
  95. NotificationType notification = sendNotificationAsync);
  96. /** Changes whether the use can type into the filename box.
  97. */
  98. void setFilenameIsEditable (bool shouldBeEditable);
  99. /** Sets a file or directory to be the default starting point for the browser to show.
  100. This is only used if the current file hasn't been set.
  101. */
  102. void setDefaultBrowseTarget (const File& newDefaultDirectory);
  103. /** This can be overridden to return a custom location that you want the dialog box
  104. to show when the browse button is pushed.
  105. The default implementation of this method will return either the current file
  106. (if one has been chosen) or the location that was set by setDefaultBrowseTarget().
  107. */
  108. virtual File getLocationToBrowse();
  109. /** Returns all the entries on the recent files list.
  110. This can be used in conjunction with setRecentlyUsedFilenames() for saving the
  111. state of this list.
  112. @see setRecentlyUsedFilenames
  113. */
  114. StringArray getRecentlyUsedFilenames() const;
  115. /** Sets all the entries on the recent files list.
  116. This can be used in conjunction with getRecentlyUsedFilenames() for saving the
  117. state of this list.
  118. @see getRecentlyUsedFilenames, addRecentlyUsedFile
  119. */
  120. void setRecentlyUsedFilenames (const StringArray& filenames);
  121. /** Adds an entry to the recently-used files dropdown list.
  122. If the file is already in the list, it will be moved to the top. A limit
  123. is also placed on the number of items that are kept in the list.
  124. @see getRecentlyUsedFilenames, setRecentlyUsedFilenames, setMaxNumberOfRecentFiles
  125. */
  126. void addRecentlyUsedFile (const File& file);
  127. /** Changes the limit for the number of files that will be stored in the recent-file list.
  128. */
  129. void setMaxNumberOfRecentFiles (int newMaximum);
  130. /** Changes the text shown on the 'browse' button.
  131. By default this button just says "..." but you can change it. The button itself
  132. can be changed using the look-and-feel classes, so it might not actually have any
  133. text on it.
  134. */
  135. void setBrowseButtonText (const String& browseButtonText);
  136. //==============================================================================
  137. /** Adds a listener that will be called when the selected file is changed. */
  138. void addListener (FilenameComponentListener* listener);
  139. /** Removes a previously-registered listener. */
  140. void removeListener (FilenameComponentListener* listener);
  141. /** Gives the component a tooltip. */
  142. void setTooltip (const String& newTooltip) override;
  143. //==============================================================================
  144. /** This abstract base class is implemented by LookAndFeel classes. */
  145. struct JUCE_API LookAndFeelMethods
  146. {
  147. virtual ~LookAndFeelMethods() {}
  148. virtual Button* createFilenameComponentBrowseButton (const String& text) = 0;
  149. virtual void layoutFilenameComponent (FilenameComponent&, ComboBox* filenameBox, Button* browseButton) = 0;
  150. };
  151. //==============================================================================
  152. /** @internal */
  153. void paintOverChildren (Graphics&) override;
  154. /** @internal */
  155. void resized() override;
  156. /** @internal */
  157. void lookAndFeelChanged() override;
  158. /** @internal */
  159. bool isInterestedInFileDrag (const StringArray&) override;
  160. /** @internal */
  161. void filesDropped (const StringArray&, int, int) override;
  162. /** @internal */
  163. void fileDragEnter (const StringArray&, int, int) override;
  164. /** @internal */
  165. void fileDragExit (const StringArray&) override;
  166. /** @internal */
  167. KeyboardFocusTraverser* createFocusTraverser() override;
  168. private:
  169. //==============================================================================
  170. ComboBox filenameBox;
  171. String lastFilename;
  172. ScopedPointer<Button> browseButton;
  173. int maxRecentFiles;
  174. bool isDir, isSaving, isFileDragOver;
  175. String wildcard, enforcedSuffix, browseButtonText;
  176. ListenerList <FilenameComponentListener> listeners;
  177. File defaultBrowseFile;
  178. void comboBoxChanged (ComboBox*) override;
  179. void buttonClicked (Button*) override;
  180. void handleAsyncUpdate() override;
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilenameComponent)
  182. };