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.

171 lines
5.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. FileChooser::FileChooser (const String& chooserBoxTitle,
  19. const File& currentFileOrDirectory,
  20. const String& fileFilters,
  21. const bool useNativeDialogBox_)
  22. : title (chooserBoxTitle),
  23. filters (fileFilters),
  24. startingFile (currentFileOrDirectory),
  25. useNativeDialogBox (useNativeDialogBox_)
  26. {
  27. if (useNativeDialogBox)
  28. {
  29. static bool canUseNativeBox = isPlatformDialogAvailable();
  30. if (! canUseNativeBox)
  31. useNativeDialogBox = false;
  32. }
  33. if (! fileFilters.containsNonWhitespaceChars())
  34. filters = "*";
  35. }
  36. FileChooser::~FileChooser()
  37. {
  38. }
  39. #if JUCE_MODAL_LOOPS_PERMITTED
  40. bool FileChooser::browseForFileToOpen (FilePreviewComponent* previewComponent)
  41. {
  42. return showDialog (false, true, false, false, false, previewComponent);
  43. }
  44. bool FileChooser::browseForMultipleFilesToOpen (FilePreviewComponent* previewComponent)
  45. {
  46. return showDialog (false, true, false, false, true, previewComponent);
  47. }
  48. bool FileChooser::browseForMultipleFilesOrDirectories (FilePreviewComponent* previewComponent)
  49. {
  50. return showDialog (true, true, false, false, true, previewComponent);
  51. }
  52. bool FileChooser::browseForFileToSave (const bool warnAboutOverwritingExistingFiles)
  53. {
  54. return showDialog (false, true, true, warnAboutOverwritingExistingFiles, false, nullptr);
  55. }
  56. bool FileChooser::browseForDirectory()
  57. {
  58. return showDialog (true, false, false, false, false, nullptr);
  59. }
  60. bool FileChooser::showDialog (const bool selectsDirectories,
  61. const bool selectsFiles,
  62. const bool isSave,
  63. const bool warnAboutOverwritingExistingFiles,
  64. const bool selectMultipleFiles,
  65. FilePreviewComponent* const previewComponent)
  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 (previewComponent == nullptr || (previewComponent->getWidth() > 10
  71. && previewComponent->getHeight() > 10));
  72. #if JUCE_WINDOWS
  73. if (useNativeDialogBox && ! (selectsFiles && selectsDirectories))
  74. #elif JUCE_MAC || JUCE_LINUX
  75. if (useNativeDialogBox && (previewComponent == nullptr))
  76. #else
  77. if (false)
  78. #endif
  79. {
  80. showPlatformDialog (results, title, startingFile, filters,
  81. selectsDirectories, selectsFiles, isSave,
  82. warnAboutOverwritingExistingFiles,
  83. selectMultipleFiles,
  84. previewComponent);
  85. }
  86. else
  87. {
  88. WildcardFileFilter wildcard (selectsFiles ? filters : String::empty,
  89. selectsDirectories ? "*" : String::empty,
  90. String::empty);
  91. int flags = isSave ? FileBrowserComponent::saveMode
  92. : FileBrowserComponent::openMode;
  93. if (selectsFiles)
  94. flags |= FileBrowserComponent::canSelectFiles;
  95. if (selectsDirectories)
  96. {
  97. flags |= FileBrowserComponent::canSelectDirectories;
  98. if (! isSave)
  99. flags |= FileBrowserComponent::filenameBoxIsReadOnly;
  100. }
  101. if (selectMultipleFiles)
  102. flags |= FileBrowserComponent::canSelectMultipleItems;
  103. FileBrowserComponent browserComponent (flags, startingFile, &wildcard, previewComponent);
  104. FileChooserDialogBox box (title, String::empty,
  105. browserComponent,
  106. warnAboutOverwritingExistingFiles,
  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. if (previouslyFocused != nullptr)
  115. previouslyFocused->grabKeyboardFocus();
  116. return results.size() > 0;
  117. }
  118. #endif
  119. File FileChooser::getResult() const
  120. {
  121. // if you've used a multiple-file select, you should use the getResults() method
  122. // to retrieve all the files that were chosen.
  123. jassert (results.size() <= 1);
  124. return results.getFirst();
  125. }
  126. const Array<File>& FileChooser::getResults() const
  127. {
  128. return results;
  129. }
  130. //==============================================================================
  131. FilePreviewComponent::FilePreviewComponent()
  132. {
  133. }
  134. FilePreviewComponent::~FilePreviewComponent()
  135. {
  136. }