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.

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