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

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