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.

150 lines
4.9KB

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