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.

228 lines
9.2KB

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