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.

juce_FileChooser.cpp 5.7KB

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