Audio plugin host https://kx.studio/carla
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.

231 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_MAC
  18. struct FileChooserDelegateClass : public ObjCClass <NSObject>
  19. {
  20. FileChooserDelegateClass() : ObjCClass <NSObject> ("JUCEFileChooser_")
  21. {
  22. addIvar<StringArray*> ("filters");
  23. addMethod (@selector (dealloc), dealloc, "v@:");
  24. addMethod (@selector (panel:shouldShowFilename:), shouldShowFilename, "c@:@@");
  25. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  26. addProtocol (@protocol (NSOpenSavePanelDelegate));
  27. #endif
  28. registerClass();
  29. }
  30. static void setFilters (id self, StringArray* filters)
  31. {
  32. object_setInstanceVariable (self, "filters", filters);
  33. }
  34. private:
  35. static void dealloc (id self, SEL)
  36. {
  37. delete getIvar<StringArray*> (self, "filters");
  38. sendSuperclassMessage (self, @selector (dealloc));
  39. }
  40. static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
  41. {
  42. StringArray* const filters = getIvar<StringArray*> (self, "filters");
  43. const File f (nsStringToJuce (filename));
  44. for (int i = filters->size(); --i >= 0;)
  45. if (f.getFileName().matchesWildcard ((*filters)[i], true))
  46. return true;
  47. #if (! defined (MAC_OS_X_VERSION_10_7)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
  48. NSError* error;
  49. NSString* name = [[NSWorkspace sharedWorkspace] typeOfFile: filename error: &error];
  50. if ([name isEqualToString: nsStringLiteral ("com.apple.alias-file")])
  51. {
  52. FSRef ref;
  53. FSPathMakeRef ((const UInt8*) [filename fileSystemRepresentation], &ref, nullptr);
  54. Boolean targetIsFolder = false, wasAliased = false;
  55. FSResolveAliasFileWithMountFlags (&ref, true, &targetIsFolder, &wasAliased, 0);
  56. return wasAliased && targetIsFolder;
  57. }
  58. #endif
  59. return f.isDirectory()
  60. && ! [[NSWorkspace sharedWorkspace] isFilePackageAtPath: filename];
  61. }
  62. };
  63. static NSMutableArray* createAllowedTypesArray (const StringArray& filters)
  64. {
  65. if (filters.size() == 0)
  66. return nil;
  67. NSMutableArray* filterArray = [[[NSMutableArray alloc] init] autorelease];
  68. for (int i = 0; i < filters.size(); ++i)
  69. {
  70. const String f (filters[i].replace ("*.", ""));
  71. if (f == "*")
  72. return nil;
  73. [filterArray addObject: juceStringToNS (f)];
  74. }
  75. return filterArray;
  76. }
  77. //==============================================================================
  78. void FileChooser::showPlatformDialog (Array<File>& results,
  79. const String& title,
  80. const File& currentFileOrDirectory,
  81. const String& filter,
  82. bool selectsDirectory,
  83. bool selectsFiles,
  84. bool isSaveDialogue,
  85. bool /*warnAboutOverwritingExistingFiles*/,
  86. bool selectMultipleFiles,
  87. FilePreviewComponent* /*extraInfoComponent*/)
  88. {
  89. JUCE_AUTORELEASEPOOL
  90. {
  91. ScopedPointer<TemporaryMainMenuWithStandardCommands> tempMenu;
  92. if (JUCEApplicationBase::isStandaloneApp())
  93. tempMenu = new TemporaryMainMenuWithStandardCommands();
  94. StringArray* filters = new StringArray();
  95. filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
  96. filters->trim();
  97. filters->removeEmptyStrings();
  98. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  99. typedef NSObject<NSOpenSavePanelDelegate> DelegateType;
  100. #else
  101. typedef NSObject DelegateType;
  102. #endif
  103. static FileChooserDelegateClass cls;
  104. DelegateType* delegate = (DelegateType*) [[cls.createInstance() init] autorelease];
  105. FileChooserDelegateClass::setFilters (delegate, filters);
  106. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  107. : [NSOpenPanel openPanel];
  108. [panel setTitle: juceStringToNS (title)];
  109. [panel setAllowedFileTypes: createAllowedTypesArray (*filters)];
  110. if (! isSaveDialogue)
  111. {
  112. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  113. [openPanel setCanChooseDirectories: selectsDirectory];
  114. [openPanel setCanChooseFiles: selectsFiles];
  115. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  116. [openPanel setResolvesAliases: YES];
  117. }
  118. [panel setDelegate: delegate];
  119. if (isSaveDialogue || selectsDirectory)
  120. [panel setCanCreateDirectories: YES];
  121. String directory, filename;
  122. if (currentFileOrDirectory.isDirectory())
  123. {
  124. directory = currentFileOrDirectory.getFullPathName();
  125. }
  126. else
  127. {
  128. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  129. filename = currentFileOrDirectory.getFileName();
  130. }
  131. #if defined (MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6)
  132. [panel setDirectoryURL: [NSURL fileURLWithPath: juceStringToNS (directory)]];
  133. [panel setNameFieldStringValue: juceStringToNS (filename)];
  134. if ([panel runModal] == NSOKButton)
  135. #else
  136. if ([panel runModalForDirectory: juceStringToNS (directory)
  137. file: juceStringToNS (filename)] == NSOKButton)
  138. #endif
  139. {
  140. if (isSaveDialogue)
  141. {
  142. results.add (File (nsStringToJuce ([[panel URL] path])));
  143. }
  144. else
  145. {
  146. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  147. NSArray* urls = [openPanel URLs];
  148. for (unsigned int i = 0; i < [urls count]; ++i)
  149. results.add (File (nsStringToJuce ([[urls objectAtIndex: i] path])));
  150. }
  151. }
  152. [panel setDelegate: nil];
  153. }
  154. }
  155. bool FileChooser::isPlatformDialogAvailable()
  156. {
  157. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  158. return false;
  159. #else
  160. return true;
  161. #endif
  162. }
  163. #else
  164. //==============================================================================
  165. bool FileChooser::isPlatformDialogAvailable()
  166. {
  167. return false;
  168. }
  169. void FileChooser::showPlatformDialog (Array<File>&,
  170. const String& /*title*/,
  171. const File& /*currentFileOrDirectory*/,
  172. const String& /*filter*/,
  173. bool /*selectsDirectory*/,
  174. bool /*selectsFiles*/,
  175. bool /*isSaveDialogue*/,
  176. bool /*warnAboutOverwritingExistingFiles*/,
  177. bool /*selectMultipleFiles*/,
  178. FilePreviewComponent*)
  179. {
  180. jassertfalse; //there's no such thing in iOS
  181. }
  182. #endif