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.

220 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_FILENAMECOMPONENT_JUCEHEADER__
  19. #define __JUCE_FILENAMECOMPONENT_JUCEHEADER__
  20. #include "../widgets/juce_ComboBox.h"
  21. #include "../buttons/juce_TextButton.h"
  22. #include "../mouse/juce_FileDragAndDropTarget.h"
  23. class FilenameComponent;
  24. //==============================================================================
  25. /**
  26. Listens for events happening to a FilenameComponent.
  27. Use FilenameComponent::addListener() and FilenameComponent::removeListener() to
  28. register one of these objects for event callbacks when the filename is changed.
  29. @see FilenameComponent
  30. */
  31. class JUCE_API FilenameComponentListener
  32. {
  33. public:
  34. /** Destructor. */
  35. virtual ~FilenameComponentListener() {}
  36. /** This method is called after the FilenameComponent's file has been changed. */
  37. virtual void filenameComponentChanged (FilenameComponent* fileComponentThatHasChanged) = 0;
  38. };
  39. //==============================================================================
  40. /**
  41. Shows a filename as an editable text box, with a 'browse' button and a
  42. drop-down list for recently selected files.
  43. A handy component for dialogue boxes where you want the user to be able to
  44. select a file or directory.
  45. Attach an FilenameComponentListener using the addListener() method, and it will
  46. get called each time the user changes the filename, either by browsing for a file
  47. and clicking 'ok', or by typing a new filename into the box and pressing return.
  48. @see FileChooser, ComboBox
  49. */
  50. class JUCE_API FilenameComponent : public Component,
  51. public SettableTooltipClient,
  52. public FileDragAndDropTarget,
  53. private AsyncUpdater,
  54. private ButtonListener, // (can't use Button::Listener due to idiotic VC2005 bug)
  55. private ComboBoxListener
  56. {
  57. public:
  58. //==============================================================================
  59. /** Creates a FilenameComponent.
  60. @param name the name for this component.
  61. @param currentFile the file to initially show in the box
  62. @param canEditFilename if true, the user can manually edit the filename; if false,
  63. they can only change it by browsing for a new file
  64. @param isDirectory if true, the file will be treated as a directory, and
  65. an appropriate directory browser used
  66. @param isForSaving if true, the file browser will allow non-existent files to
  67. be picked, as the file is assumed to be used for saving rather
  68. than loading
  69. @param fileBrowserWildcard a wildcard pattern to use in the file browser - e.g. "*.txt;*.foo".
  70. If an empty string is passed in, then the pattern is assumed to be "*"
  71. @param enforcedSuffix if this is non-empty, it is treated as a suffix that will be added
  72. to any filenames that are entered or chosen
  73. @param textWhenNothingSelected the message to display in the box before any filename is entered. (This
  74. will only appear if the initial file isn't valid)
  75. */
  76. FilenameComponent (const String& name,
  77. const File& currentFile,
  78. bool canEditFilename,
  79. bool isDirectory,
  80. bool isForSaving,
  81. const String& fileBrowserWildcard,
  82. const String& enforcedSuffix,
  83. const String& textWhenNothingSelected);
  84. /** Destructor. */
  85. ~FilenameComponent();
  86. //==============================================================================
  87. /** Returns the currently displayed filename. */
  88. File getCurrentFile() const;
  89. /** Changes the current filename.
  90. If addToRecentlyUsedList is true, the filename will also be added to the
  91. drop-down list of recent files.
  92. If sendChangeNotification is false, then the listeners won't be told of the
  93. change.
  94. */
  95. void setCurrentFile (File newFile,
  96. bool addToRecentlyUsedList,
  97. bool sendChangeNotification = true);
  98. /** Changes whether the use can type into the filename box.
  99. */
  100. void setFilenameIsEditable (bool shouldBeEditable);
  101. /** Sets a file or directory to be the default starting point for the browser to show.
  102. This is only used if the current file hasn't been set.
  103. */
  104. void setDefaultBrowseTarget (const File& newDefaultDirectory);
  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);
  139. //==============================================================================
  140. /** @internal */
  141. void paintOverChildren (Graphics& g);
  142. /** @internal */
  143. void resized();
  144. /** @internal */
  145. void lookAndFeelChanged();
  146. /** @internal */
  147. bool isInterestedInFileDrag (const StringArray& files);
  148. /** @internal */
  149. void filesDropped (const StringArray& files, int, int);
  150. /** @internal */
  151. void fileDragEnter (const StringArray& files, int, int);
  152. /** @internal */
  153. void fileDragExit (const StringArray& files);
  154. private:
  155. //==============================================================================
  156. ComboBox filenameBox;
  157. String lastFilename;
  158. ScopedPointer<Button> browseButton;
  159. int maxRecentFiles;
  160. bool isDir, isSaving, isFileDragOver;
  161. String wildcard, enforcedSuffix, browseButtonText;
  162. ListenerList <FilenameComponentListener> listeners;
  163. File defaultBrowseFile;
  164. void comboBoxChanged (ComboBox*);
  165. void buttonClicked (Button* button);
  166. void handleAsyncUpdate();
  167. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FilenameComponent);
  168. };
  169. #endif // __JUCE_FILENAMECOMPONENT_JUCEHEADER__