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.

177 lines
6.0KB

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