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.

269 lines
10KB

  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. addIvar<FilePreviewComponent*> ("filePreviewComponent");
  24. addMethod (@selector (dealloc), dealloc, "v@:");
  25. addMethod (@selector (panel:shouldShowFilename:), shouldShowFilename, "c@:@@");
  26. addMethod (@selector (panelSelectionDidChange:), panelSelectionDidChange, "c@");
  27. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  28. addProtocol (@protocol (NSOpenSavePanelDelegate));
  29. #endif
  30. registerClass();
  31. }
  32. static void setFilters (id self, StringArray* filters) { object_setInstanceVariable (self, "filters", filters); }
  33. static void setFilePreviewComponent (id self, FilePreviewComponent* comp) { object_setInstanceVariable (self, "filePreviewComponent", comp); }
  34. static StringArray* getFilters (id self) { return getIvar<StringArray*> (self, "filters"); }
  35. static FilePreviewComponent* getFilePreviewComponent (id self) { return getIvar<FilePreviewComponent*> (self, "filePreviewComponent"); }
  36. private:
  37. static void dealloc (id self, SEL)
  38. {
  39. delete getFilters (self);
  40. sendSuperclassMessage (self, @selector (dealloc));
  41. }
  42. static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
  43. {
  44. StringArray* const filters = getFilters (self);
  45. const File f (nsStringToJuce (filename));
  46. for (int i = filters->size(); --i >= 0;)
  47. if (f.getFileName().matchesWildcard ((*filters)[i], true))
  48. return true;
  49. #if (! defined (MAC_OS_X_VERSION_10_7)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
  50. NSError* error;
  51. NSString* name = [[NSWorkspace sharedWorkspace] typeOfFile: filename error: &error];
  52. if ([name isEqualToString: nsStringLiteral ("com.apple.alias-file")])
  53. {
  54. FSRef ref;
  55. FSPathMakeRef ((const UInt8*) [filename fileSystemRepresentation], &ref, nullptr);
  56. Boolean targetIsFolder = false, wasAliased = false;
  57. FSResolveAliasFileWithMountFlags (&ref, true, &targetIsFolder, &wasAliased, 0);
  58. return wasAliased && targetIsFolder;
  59. }
  60. #endif
  61. return f.isDirectory()
  62. && ! [[NSWorkspace sharedWorkspace] isFilePackageAtPath: filename];
  63. }
  64. static StringArray getSelectedPaths (id sender)
  65. {
  66. StringArray paths;
  67. if ([sender isKindOfClass: [NSOpenPanel class]])
  68. {
  69. NSArray* urls = [(NSOpenPanel*) sender URLs];
  70. for (NSUInteger i = 0; i < [urls count]; ++i)
  71. paths.add (nsStringToJuce ([[urls objectAtIndex: i] path]));
  72. }
  73. else if ([sender isKindOfClass: [NSSavePanel class]])
  74. {
  75. paths.add (nsStringToJuce ([[(NSSavePanel*) sender URL] path]));
  76. }
  77. return paths;
  78. }
  79. static void panelSelectionDidChange (id self, SEL, id sender)
  80. {
  81. // NB: would need to extend FilePreviewComponent to handle the full list rather than just the first one
  82. if (FilePreviewComponent* const previewComp = getFilePreviewComponent (self))
  83. previewComp->selectedFileChanged (File (getSelectedPaths (sender)[0]));
  84. }
  85. };
  86. static NSMutableArray* createAllowedTypesArray (const StringArray& filters)
  87. {
  88. if (filters.size() == 0)
  89. return nil;
  90. NSMutableArray* filterArray = [[[NSMutableArray alloc] init] autorelease];
  91. for (int i = 0; i < filters.size(); ++i)
  92. {
  93. const String f (filters[i].replace ("*.", ""));
  94. if (f == "*")
  95. return nil;
  96. [filterArray addObject: juceStringToNS (f)];
  97. }
  98. return filterArray;
  99. }
  100. //==============================================================================
  101. void FileChooser::showPlatformDialog (Array<File>& results,
  102. const String& title,
  103. const File& currentFileOrDirectory,
  104. const String& filter,
  105. bool selectsDirectory,
  106. bool selectsFiles,
  107. bool isSaveDialogue,
  108. bool /*warnAboutOverwritingExistingFiles*/,
  109. bool selectMultipleFiles,
  110. FilePreviewComponent* extraInfoComponent)
  111. {
  112. JUCE_AUTORELEASEPOOL
  113. {
  114. ScopedPointer<TemporaryMainMenuWithStandardCommands> tempMenu;
  115. if (JUCEApplicationBase::isStandaloneApp())
  116. tempMenu = new TemporaryMainMenuWithStandardCommands();
  117. StringArray* filters = new StringArray();
  118. filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
  119. filters->trim();
  120. filters->removeEmptyStrings();
  121. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  122. typedef NSObject<NSOpenSavePanelDelegate> DelegateType;
  123. #else
  124. typedef NSObject DelegateType;
  125. #endif
  126. static FileChooserDelegateClass cls;
  127. DelegateType* delegate = (DelegateType*) [[cls.createInstance() init] autorelease];
  128. FileChooserDelegateClass::setFilters (delegate, filters);
  129. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  130. : [NSOpenPanel openPanel];
  131. [panel setTitle: juceStringToNS (title)];
  132. [panel setAllowedFileTypes: createAllowedTypesArray (*filters)];
  133. if (! isSaveDialogue)
  134. {
  135. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  136. [openPanel setCanChooseDirectories: selectsDirectory];
  137. [openPanel setCanChooseFiles: selectsFiles];
  138. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  139. [openPanel setResolvesAliases: YES];
  140. }
  141. if (extraInfoComponent != nullptr)
  142. {
  143. NSView* view = [[[NSView alloc] initWithFrame: makeNSRect (extraInfoComponent->getLocalBounds())] autorelease];
  144. extraInfoComponent->addToDesktop (0, (void*) view);
  145. extraInfoComponent->setVisible (true);
  146. FileChooserDelegateClass::setFilePreviewComponent (delegate, extraInfoComponent);
  147. [panel setAccessoryView: view];
  148. }
  149. [panel setDelegate: delegate];
  150. if (isSaveDialogue || selectsDirectory)
  151. [panel setCanCreateDirectories: YES];
  152. String directory, filename;
  153. if (currentFileOrDirectory.isDirectory())
  154. {
  155. directory = currentFileOrDirectory.getFullPathName();
  156. }
  157. else
  158. {
  159. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  160. filename = currentFileOrDirectory.getFileName();
  161. }
  162. #if defined (MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6)
  163. [panel setDirectoryURL: [NSURL fileURLWithPath: juceStringToNS (directory)]];
  164. [panel setNameFieldStringValue: juceStringToNS (filename)];
  165. if ([panel runModal] == 1 /*NSModalResponseOK*/)
  166. #else
  167. if ([panel runModalForDirectory: juceStringToNS (directory)
  168. file: juceStringToNS (filename)] == 1 /*NSModalResponseOK*/)
  169. #endif
  170. {
  171. if (isSaveDialogue)
  172. {
  173. results.add (File (nsStringToJuce ([[panel URL] path])));
  174. }
  175. else
  176. {
  177. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  178. NSArray* urls = [openPanel URLs];
  179. for (unsigned int i = 0; i < [urls count]; ++i)
  180. results.add (File (nsStringToJuce ([[urls objectAtIndex: i] path])));
  181. }
  182. }
  183. [panel setDelegate: nil];
  184. }
  185. }
  186. bool FileChooser::isPlatformDialogAvailable()
  187. {
  188. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  189. return false;
  190. #else
  191. return true;
  192. #endif
  193. }
  194. #else
  195. //==============================================================================
  196. bool FileChooser::isPlatformDialogAvailable()
  197. {
  198. return false;
  199. }
  200. void FileChooser::showPlatformDialog (Array<File>&,
  201. const String& /*title*/,
  202. const File& /*currentFileOrDirectory*/,
  203. const String& /*filter*/,
  204. bool /*selectsDirectory*/,
  205. bool /*selectsFiles*/,
  206. bool /*isSaveDialogue*/,
  207. bool /*warnAboutOverwritingExistingFiles*/,
  208. bool /*selectMultipleFiles*/,
  209. FilePreviewComponent*)
  210. {
  211. jassertfalse; //there's no such thing in iOS
  212. }
  213. #endif