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.

153 lines
5.0KB

  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. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  24. // compiled on its own).
  25. #ifdef JUCE_INCLUDED_FILE
  26. //==============================================================================
  27. END_JUCE_NAMESPACE
  28. using namespace JUCE_NAMESPACE;
  29. #define JuceFileChooserDelegate MakeObjCClassName(JuceFileChooserDelegate)
  30. @interface JuceFileChooserDelegate : NSObject
  31. {
  32. StringArray* filters;
  33. }
  34. - (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_;
  35. - (void) dealloc;
  36. - (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename;
  37. @end
  38. @implementation JuceFileChooserDelegate
  39. - (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_
  40. {
  41. [super init];
  42. filters = filters_;
  43. return self;
  44. }
  45. - (void) dealloc
  46. {
  47. delete filters;
  48. [super dealloc];
  49. }
  50. - (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename
  51. {
  52. const String fname (nsStringToJuce (filename));
  53. for (int i = filters->size(); --i >= 0;)
  54. if (fname.matchesWildcard ((*filters)[i], true))
  55. return true;
  56. return File (fname).isDirectory();
  57. }
  58. @end
  59. BEGIN_JUCE_NAMESPACE
  60. //==============================================================================
  61. void FileChooser::showPlatformDialog (OwnedArray<File>& results,
  62. const String& title,
  63. const File& currentFileOrDirectory,
  64. const String& filter,
  65. bool selectsDirectory,
  66. bool isSaveDialogue,
  67. bool warnAboutOverwritingExistingFiles,
  68. bool selectMultipleFiles,
  69. FilePreviewComponent* extraInfoComponent)
  70. {
  71. const ScopedAutoReleasePool pool;
  72. StringArray* filters = new StringArray();
  73. filters->addTokens (filter.replaceCharacters (T(",:"), T(";;")), T(";"), 0);
  74. filters->trim();
  75. filters->removeEmptyStrings();
  76. JuceFileChooserDelegate* delegate = [[JuceFileChooserDelegate alloc] initWithFilters: filters];
  77. [delegate autorelease];
  78. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  79. : [NSOpenPanel openPanel];
  80. [panel setTitle: juceStringToNS (title)];
  81. if (! isSaveDialogue)
  82. {
  83. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  84. [openPanel setCanChooseDirectories: selectsDirectory];
  85. [openPanel setCanChooseFiles: ! selectsDirectory];
  86. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  87. }
  88. [panel setDelegate: delegate];
  89. String directory, filename;
  90. if (currentFileOrDirectory.isDirectory())
  91. {
  92. directory = currentFileOrDirectory.getFullPathName();
  93. }
  94. else
  95. {
  96. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  97. filename = currentFileOrDirectory.getFileName();
  98. }
  99. if ([panel runModalForDirectory: juceStringToNS (directory)
  100. file: juceStringToNS (filename)]
  101. == NSOKButton)
  102. {
  103. if (isSaveDialogue)
  104. {
  105. results.add (new File (nsStringToJuce ([panel filename])));
  106. }
  107. else
  108. {
  109. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  110. NSArray* urls = [openPanel filenames];
  111. for (unsigned int i = 0; i < [urls count]; ++i)
  112. {
  113. NSString* f = [urls objectAtIndex: i];
  114. results.add (new File (nsStringToJuce (f)));
  115. }
  116. }
  117. }
  118. [panel setDelegate: nil];
  119. }
  120. #endif