The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

218 lines
8.9KB

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