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.

147 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  19. // compiled on its own).
  20. #ifdef JUCE_INCLUDED_FILE
  21. //==============================================================================
  22. END_JUCE_NAMESPACE
  23. using namespace JUCE_NAMESPACE;
  24. #define JuceFileChooserDelegate MakeObjCClassName(JuceFileChooserDelegate)
  25. @interface JuceFileChooserDelegate : NSObject
  26. {
  27. StringArray* filters;
  28. }
  29. - (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_;
  30. - (void) dealloc;
  31. - (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename;
  32. @end
  33. @implementation JuceFileChooserDelegate
  34. - (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_
  35. {
  36. [super init];
  37. filters = filters_;
  38. return self;
  39. }
  40. - (void) dealloc
  41. {
  42. delete filters;
  43. [super dealloc];
  44. }
  45. - (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename
  46. {
  47. const String fname (nsStringToJuce (filename));
  48. for (int i = filters->size(); --i >= 0;)
  49. if (fname.matchesWildcard ((*filters)[i], true))
  50. return true;
  51. return File (fname).isDirectory();
  52. }
  53. @end
  54. BEGIN_JUCE_NAMESPACE
  55. //==============================================================================
  56. void FileChooser::showPlatformDialog (OwnedArray<File>& results,
  57. const String& title,
  58. const File& currentFileOrDirectory,
  59. const String& filter,
  60. bool selectsDirectory,
  61. bool isSaveDialogue,
  62. bool warnAboutOverwritingExistingFiles,
  63. bool selectMultipleFiles,
  64. FilePreviewComponent* extraInfoComponent)
  65. {
  66. const ScopedAutoReleasePool pool;
  67. StringArray* filters = new StringArray();
  68. filters->addTokens (filter.replaceCharacters (T(",:"), T(";;")), T(";"), 0);
  69. filters->trim();
  70. filters->removeEmptyStrings();
  71. JuceFileChooserDelegate* delegate = [[JuceFileChooserDelegate alloc] initWithFilters: filters];
  72. [delegate autorelease];
  73. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  74. : [NSOpenPanel openPanel];
  75. [panel setTitle: juceStringToNS (title)];
  76. if (! isSaveDialogue)
  77. {
  78. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  79. [openPanel setCanChooseDirectories: selectsDirectory];
  80. [openPanel setCanChooseFiles: ! selectsDirectory];
  81. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  82. }
  83. [panel setDelegate: delegate];
  84. String directory, filename;
  85. if (currentFileOrDirectory.isDirectory())
  86. {
  87. directory = currentFileOrDirectory.getFullPathName();
  88. }
  89. else
  90. {
  91. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  92. filename = currentFileOrDirectory.getFileName();
  93. }
  94. if ([panel runModalForDirectory: juceStringToNS (directory)
  95. file: juceStringToNS (filename)]
  96. == NSOKButton)
  97. {
  98. if (isSaveDialogue)
  99. {
  100. results.add (new File (nsStringToJuce ([panel filename])));
  101. }
  102. else
  103. {
  104. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  105. NSArray* urls = [openPanel filenames];
  106. for (unsigned int i = 0; i < [urls count]; ++i)
  107. {
  108. NSString* f = [urls objectAtIndex: i];
  109. results.add (new File (nsStringToJuce (f)));
  110. }
  111. }
  112. }
  113. [panel setDelegate: nil];
  114. }
  115. #endif