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.

158 lines
5.8KB

  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. namespace juce
  20. {
  21. FileChooser::FileChooser (const String& chooserBoxTitle,
  22. const File& currentFileOrDirectory,
  23. const String& fileFilters,
  24. const bool useNativeBox,
  25. const bool treatFilePackagesAsDirectories)
  26. : title (chooserBoxTitle),
  27. filters (fileFilters),
  28. startingFile (currentFileOrDirectory),
  29. useNativeDialogBox (useNativeBox && isPlatformDialogAvailable()),
  30. treatFilePackagesAsDirs (treatFilePackagesAsDirectories)
  31. {
  32. if (! fileFilters.containsNonWhitespaceChars())
  33. filters = "*";
  34. }
  35. FileChooser::~FileChooser() {}
  36. #if JUCE_MODAL_LOOPS_PERMITTED
  37. bool FileChooser::browseForFileToOpen (FilePreviewComponent* previewComp)
  38. {
  39. return showDialog (FileBrowserComponent::openMode
  40. | FileBrowserComponent::canSelectFiles,
  41. previewComp);
  42. }
  43. bool FileChooser::browseForMultipleFilesToOpen (FilePreviewComponent* previewComp)
  44. {
  45. return showDialog (FileBrowserComponent::openMode
  46. | FileBrowserComponent::canSelectFiles
  47. | FileBrowserComponent::canSelectMultipleItems,
  48. previewComp);
  49. }
  50. bool FileChooser::browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComp)
  51. {
  52. return showDialog (FileBrowserComponent::openMode
  53. | FileBrowserComponent::canSelectFiles
  54. | FileBrowserComponent::canSelectDirectories
  55. | FileBrowserComponent::canSelectMultipleItems,
  56. previewComp);
  57. }
  58. bool FileChooser::browseForFileToSave (const bool warnAboutOverwrite)
  59. {
  60. return showDialog (FileBrowserComponent::saveMode
  61. | FileBrowserComponent::canSelectFiles
  62. | (warnAboutOverwrite ? FileBrowserComponent::warnAboutOverwriting : 0),
  63. nullptr);
  64. }
  65. bool FileChooser::browseForDirectory()
  66. {
  67. return showDialog (FileBrowserComponent::openMode
  68. | FileBrowserComponent::canSelectDirectories,
  69. nullptr);
  70. }
  71. bool FileChooser::showDialog (const int flags, FilePreviewComponent* const previewComp)
  72. {
  73. FocusRestorer focusRestorer;
  74. results.clear();
  75. // the preview component needs to be the right size before you pass it in here..
  76. jassert (previewComp == nullptr || (previewComp->getWidth() > 10
  77. && previewComp->getHeight() > 10));
  78. const bool selectsDirectories = (flags & FileBrowserComponent::canSelectDirectories) != 0;
  79. const bool selectsFiles = (flags & FileBrowserComponent::canSelectFiles) != 0;
  80. const bool isSave = (flags & FileBrowserComponent::saveMode) != 0;
  81. const bool warnAboutOverwrite = (flags & FileBrowserComponent::warnAboutOverwriting) != 0;
  82. const bool selectMultiple = (flags & FileBrowserComponent::canSelectMultipleItems) != 0;
  83. // You've set the flags for both saveMode and openMode!
  84. jassert (! (isSave && (flags & FileBrowserComponent::openMode) != 0));
  85. #if JUCE_WINDOWS
  86. if (useNativeDialogBox && ! (selectsFiles && selectsDirectories))
  87. #elif JUCE_MAC || JUCE_LINUX
  88. if (useNativeDialogBox)
  89. #else
  90. if (false)
  91. #endif
  92. {
  93. showPlatformDialog (results, title, startingFile, filters,
  94. selectsDirectories, selectsFiles, isSave,
  95. warnAboutOverwrite, selectMultiple, treatFilePackagesAsDirs,
  96. previewComp);
  97. }
  98. else
  99. {
  100. ignoreUnused (selectMultiple);
  101. WildcardFileFilter wildcard (selectsFiles ? filters : String(),
  102. selectsDirectories ? "*" : String(),
  103. String());
  104. FileBrowserComponent browserComponent (flags, startingFile, &wildcard, previewComp);
  105. FileChooserDialogBox box (title, String(),
  106. browserComponent, warnAboutOverwrite,
  107. browserComponent.findColour (AlertWindow::backgroundColourId));
  108. if (box.show())
  109. {
  110. for (int i = 0; i < browserComponent.getNumSelectedFiles(); ++i)
  111. results.add (browserComponent.getSelectedFile (i));
  112. }
  113. }
  114. return results.size() > 0;
  115. }
  116. #endif
  117. File FileChooser::getResult() const
  118. {
  119. // if you've used a multiple-file select, you should use the getResults() method
  120. // to retrieve all the files that were chosen.
  121. jassert (results.size() <= 1);
  122. return results.getFirst();
  123. }
  124. //==============================================================================
  125. FilePreviewComponent::FilePreviewComponent() {}
  126. FilePreviewComponent::~FilePreviewComponent() {}
  127. } // namespace juce