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.

176 lines
5.9KB

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