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.

265 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. class FileChooser::NonNative : public FileChooser::Pimpl
  17. {
  18. public:
  19. NonNative (FileChooser& fileChooser, int flags, FilePreviewComponent* preview)
  20. : owner (fileChooser),
  21. selectsDirectories ((flags & FileBrowserComponent::canSelectDirectories) != 0),
  22. selectsFiles ((flags & FileBrowserComponent::canSelectFiles) != 0),
  23. warnAboutOverwrite ((flags & FileBrowserComponent::warnAboutOverwriting) != 0),
  24. filter (selectsFiles ? owner.filters : String(), selectsDirectories ? "*" : String(), {}),
  25. browserComponent (flags, owner.startingFile, &filter, preview),
  26. dialogBox (owner.title, {}, browserComponent, warnAboutOverwrite,
  27. browserComponent.findColour (AlertWindow::backgroundColourId), owner.parent)
  28. {}
  29. ~NonNative() override
  30. {
  31. dialogBox.exitModalState (0);
  32. }
  33. void launch() override
  34. {
  35. dialogBox.centreWithDefaultSize (nullptr);
  36. dialogBox.enterModalState (true, ModalCallbackFunction::create ([this] (int r) { modalStateFinished (r); }), true);
  37. }
  38. void runModally() override
  39. {
  40. #if JUCE_MODAL_LOOPS_PERMITTED
  41. modalStateFinished (dialogBox.show() ? 1 : 0);
  42. #else
  43. jassertfalse;
  44. #endif
  45. }
  46. private:
  47. void modalStateFinished (int returnValue)
  48. {
  49. Array<URL> result;
  50. if (returnValue != 0)
  51. {
  52. for (int i = 0; i < browserComponent.getNumSelectedFiles(); ++i)
  53. result.add (URL (browserComponent.getSelectedFile (i)));
  54. }
  55. owner.finished (result);
  56. }
  57. //==============================================================================
  58. FileChooser& owner;
  59. bool selectsDirectories, selectsFiles, warnAboutOverwrite;
  60. WildcardFileFilter filter;
  61. FileBrowserComponent browserComponent;
  62. FileChooserDialogBox dialogBox;
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NonNative)
  64. };
  65. //==============================================================================
  66. FileChooser::FileChooser (const String& chooserBoxTitle,
  67. const File& currentFileOrDirectory,
  68. const String& fileFilters,
  69. const bool useNativeBox,
  70. const bool treatFilePackagesAsDirectories,
  71. Component* parentComponentToUse)
  72. : title (chooserBoxTitle),
  73. filters (fileFilters),
  74. startingFile (currentFileOrDirectory),
  75. parent (parentComponentToUse),
  76. useNativeDialogBox (useNativeBox && isPlatformDialogAvailable()),
  77. treatFilePackagesAsDirs (treatFilePackagesAsDirectories)
  78. {
  79. #ifndef JUCE_MAC
  80. ignoreUnused (treatFilePackagesAsDirs);
  81. #endif
  82. if (! fileFilters.containsNonWhitespaceChars())
  83. filters = "*";
  84. }
  85. FileChooser::~FileChooser()
  86. {
  87. asyncCallback = nullptr;
  88. }
  89. #if JUCE_MODAL_LOOPS_PERMITTED
  90. bool FileChooser::browseForFileToOpen (FilePreviewComponent* previewComp)
  91. {
  92. return showDialog (FileBrowserComponent::openMode
  93. | FileBrowserComponent::canSelectFiles,
  94. previewComp);
  95. }
  96. bool FileChooser::browseForMultipleFilesToOpen (FilePreviewComponent* previewComp)
  97. {
  98. return showDialog (FileBrowserComponent::openMode
  99. | FileBrowserComponent::canSelectFiles
  100. | FileBrowserComponent::canSelectMultipleItems,
  101. previewComp);
  102. }
  103. bool FileChooser::browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComp)
  104. {
  105. return showDialog (FileBrowserComponent::openMode
  106. | FileBrowserComponent::canSelectFiles
  107. | FileBrowserComponent::canSelectDirectories
  108. | FileBrowserComponent::canSelectMultipleItems,
  109. previewComp);
  110. }
  111. bool FileChooser::browseForFileToSave (const bool warnAboutOverwrite)
  112. {
  113. return showDialog (FileBrowserComponent::saveMode
  114. | FileBrowserComponent::canSelectFiles
  115. | (warnAboutOverwrite ? FileBrowserComponent::warnAboutOverwriting : 0),
  116. nullptr);
  117. }
  118. bool FileChooser::browseForDirectory()
  119. {
  120. return showDialog (FileBrowserComponent::openMode
  121. | FileBrowserComponent::canSelectDirectories,
  122. nullptr);
  123. }
  124. bool FileChooser::showDialog (const int flags, FilePreviewComponent* const previewComp)
  125. {
  126. FocusRestorer focusRestorer;
  127. pimpl.reset (createPimpl (flags, previewComp));
  128. pimpl->runModally();
  129. // ensure that the finished function was invoked
  130. jassert (pimpl == nullptr);
  131. return (results.size() > 0);
  132. }
  133. #endif
  134. void FileChooser::launchAsync (int flags, std::function<void(const FileChooser&)> callback,
  135. FilePreviewComponent* previewComp)
  136. {
  137. // You must specify a callback when using launchAsync
  138. jassert (callback);
  139. // you cannot run two file chooser dialog boxes at the same time
  140. jassert (asyncCallback == nullptr);
  141. asyncCallback = std::move (callback);
  142. pimpl.reset (createPimpl (flags, previewComp));
  143. pimpl->launch();
  144. }
  145. FileChooser::Pimpl* FileChooser::createPimpl (int flags, FilePreviewComponent* previewComp)
  146. {
  147. results.clear();
  148. // the preview component needs to be the right size before you pass it in here..
  149. jassert (previewComp == nullptr || (previewComp->getWidth() > 10
  150. && previewComp->getHeight() > 10));
  151. if (pimpl != nullptr)
  152. {
  153. // you cannot run two file chooser dialog boxes at the same time
  154. jassertfalse;
  155. pimpl.reset();
  156. }
  157. // You've set the flags for both saveMode and openMode!
  158. jassert (! (((flags & FileBrowserComponent::saveMode) != 0)
  159. && ((flags & FileBrowserComponent::openMode) != 0)));
  160. #if JUCE_WINDOWS
  161. const bool selectsFiles = (flags & FileBrowserComponent::canSelectFiles) != 0;
  162. const bool selectsDirectories = (flags & FileBrowserComponent::canSelectDirectories) != 0;
  163. if (useNativeDialogBox && ! (selectsFiles && selectsDirectories))
  164. #else
  165. if (useNativeDialogBox)
  166. #endif
  167. {
  168. return showPlatformDialog (*this, flags, previewComp);
  169. }
  170. else
  171. {
  172. return new NonNative (*this, flags, previewComp);
  173. }
  174. }
  175. Array<File> FileChooser::getResults() const noexcept
  176. {
  177. Array<File> files;
  178. for (auto url : getURLResults())
  179. if (url.isLocalFile())
  180. files.add (url.getLocalFile());
  181. return files;
  182. }
  183. File FileChooser::getResult() const
  184. {
  185. auto fileResults = getResults();
  186. // if you've used a multiple-file select, you should use the getResults() method
  187. // to retrieve all the files that were chosen.
  188. jassert (fileResults.size() <= 1);
  189. return fileResults.getFirst();
  190. }
  191. URL FileChooser::getURLResult() const
  192. {
  193. // if you've used a multiple-file select, you should use the getResults() method
  194. // to retrieve all the files that were chosen.
  195. jassert (results.size() <= 1);
  196. return results.getFirst();
  197. }
  198. void FileChooser::finished (const Array<URL>& asyncResults)
  199. {
  200. std::function<void(const FileChooser&)> callback;
  201. std::swap (callback, asyncCallback);
  202. results = asyncResults;
  203. pimpl.reset();
  204. if (callback)
  205. callback (*this);
  206. }
  207. //==============================================================================
  208. FilePreviewComponent::FilePreviewComponent() {}
  209. FilePreviewComponent::~FilePreviewComponent() {}
  210. } // namespace juce