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.

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