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.

142 lines
4.7KB

  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].toLowerCase());
  55. if (fnmatch (wildcard.toUTF8(), filenameUTF8, 0) == 0)
  56. return true;
  57. }
  58. return JUCE_NAMESPACE::File (nsStringToJuce (filename)).isDirectory();
  59. }
  60. @end
  61. BEGIN_JUCE_NAMESPACE
  62. void FileChooser::showPlatformDialog (OwnedArray<File>& results,
  63. const String& title,
  64. const File& currentFileOrDirectory,
  65. const String& filter,
  66. bool selectsDirectory,
  67. bool isSaveDialogue,
  68. bool warnAboutOverwritingExistingFiles,
  69. bool selectMultipleFiles,
  70. FilePreviewComponent* extraInfoComponent)
  71. {
  72. const AutoPool pool;
  73. StringArray* filters = new StringArray();
  74. filters->addTokens (filter.replaceCharacters (T(",:"), T(";;")), T(";"), 0);
  75. filters->trim();
  76. filters->removeEmptyStrings();
  77. JuceFileChooserDelegate* delegate = [[JuceFileChooserDelegate alloc] initWithFilters: filters];
  78. [delegate autorelease];
  79. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  80. : [NSOpenPanel openPanel];
  81. [panel setTitle: juceStringToNS (title)];
  82. if (! isSaveDialogue)
  83. {
  84. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  85. [openPanel setCanChooseDirectories: selectsDirectory];
  86. [openPanel setCanChooseFiles: ! selectsDirectory];
  87. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  88. }
  89. [panel setDelegate: delegate];
  90. if ([panel runModalForDirectory: juceStringToNS (currentFileOrDirectory.getParentDirectory().getFullPathName())
  91. file: juceStringToNS (currentFileOrDirectory.getFileName())]
  92. == NSOKButton)
  93. {
  94. if (isSaveDialogue)
  95. {
  96. results.add (new File (nsStringToJuce ([panel filename])));
  97. }
  98. else
  99. {
  100. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  101. NSArray* urls = [openPanel filenames];
  102. for (int i = 0; i < [urls count]; ++i)
  103. {
  104. NSString* f = [urls objectAtIndex: i];
  105. results.add (new File (nsStringToJuce (f)));
  106. }
  107. }
  108. }
  109. }
  110. END_JUCE_NAMESPACE