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.

173 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../../../../juce_core/basics/juce_StandardHeader.h"
  24. BEGIN_JUCE_NAMESPACE
  25. #include "juce_FileChooser.h"
  26. #include "juce_WildcardFileFilter.h"
  27. #include "juce_FileChooserDialogBox.h"
  28. #include "../lookandfeel/juce_LookAndFeel.h"
  29. #include "../windows/juce_AlertWindow.h"
  30. //==============================================================================
  31. FileChooser::FileChooser (const String& chooserBoxTitle,
  32. const File& currentFileOrDirectory,
  33. const String& fileFilters,
  34. const bool useNativeDialogBox_)
  35. : title (chooserBoxTitle),
  36. filters (fileFilters),
  37. startingFile (currentFileOrDirectory),
  38. useNativeDialogBox (useNativeDialogBox_)
  39. {
  40. #if JUCE_LINUX
  41. useNativeDialogBox = false;
  42. #endif
  43. if (fileFilters.trim().isEmpty())
  44. filters = T("*");
  45. }
  46. FileChooser::~FileChooser()
  47. {
  48. }
  49. bool FileChooser::browseForFileToOpen (FilePreviewComponent* previewComponent)
  50. {
  51. return showDialog (false, false, false, false, previewComponent);
  52. }
  53. bool FileChooser::browseForMultipleFilesToOpen (FilePreviewComponent* previewComponent)
  54. {
  55. return showDialog (false, false, false, true, previewComponent);
  56. }
  57. bool FileChooser::browseForFileToSave (const bool warnAboutOverwritingExistingFiles)
  58. {
  59. return showDialog (false, true, warnAboutOverwritingExistingFiles, false, 0);
  60. }
  61. bool FileChooser::browseForDirectory()
  62. {
  63. return showDialog (true, false, false, false, 0);
  64. }
  65. const File FileChooser::getResult() const
  66. {
  67. // if you've used a multiple-file select, you should use the getResults() method
  68. // to retrieve all the files that were chosen.
  69. jassert (results.size() <= 1);
  70. const File* const f = results.getFirst();
  71. if (f != 0)
  72. return *f;
  73. return File::nonexistent;
  74. }
  75. const OwnedArray <File>& FileChooser::getResults() const
  76. {
  77. return results;
  78. }
  79. bool FileChooser::showDialog (const bool isDirectory,
  80. const bool isSave,
  81. const bool warnAboutOverwritingExistingFiles,
  82. const bool selectMultipleFiles,
  83. FilePreviewComponent* const previewComponent)
  84. {
  85. ComponentDeletionWatcher* currentlyFocusedChecker = 0;
  86. Component* const currentlyFocused = Component::getCurrentlyFocusedComponent();
  87. if (currentlyFocused != 0)
  88. currentlyFocusedChecker = new ComponentDeletionWatcher (currentlyFocused);
  89. results.clear();
  90. // the preview component needs to be the right size before you pass it in here..
  91. jassert (previewComponent == 0 || (previewComponent->getWidth() > 10
  92. && previewComponent->getHeight() > 10));
  93. #if JUCE_WIN32
  94. if (useNativeDialogBox)
  95. #else
  96. if (useNativeDialogBox && (previewComponent == 0))
  97. #endif
  98. {
  99. showPlatformDialog (results, title, startingFile, filters,
  100. isDirectory, isSave,
  101. warnAboutOverwritingExistingFiles,
  102. selectMultipleFiles,
  103. previewComponent);
  104. }
  105. else
  106. {
  107. jassert (! selectMultipleFiles); // not yet implemented for juce dialogs!
  108. WildcardFileFilter wildcard (filters, String::empty);
  109. FileBrowserComponent browserComponent (isDirectory ? FileBrowserComponent::chooseDirectoryMode
  110. : (isSave ? FileBrowserComponent::saveFileMode
  111. : FileBrowserComponent::loadFileMode),
  112. startingFile, &wildcard, previewComponent);
  113. FileChooserDialogBox box (title, String::empty,
  114. browserComponent,
  115. warnAboutOverwritingExistingFiles,
  116. browserComponent.findColour (AlertWindow::backgroundColourId));
  117. if (box.show())
  118. results.add (new File (browserComponent.getCurrentFile()));
  119. }
  120. if (currentlyFocused != 0 && ! currentlyFocusedChecker->hasBeenDeleted())
  121. currentlyFocused->grabKeyboardFocus();
  122. delete currentlyFocusedChecker;
  123. return results.size() > 0;
  124. }
  125. //==============================================================================
  126. FilePreviewComponent::FilePreviewComponent()
  127. {
  128. }
  129. FilePreviewComponent::~FilePreviewComponent()
  130. {
  131. }
  132. END_JUCE_NAMESPACE