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.

229 lines
8.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. #if JUCE_MAC
  19. struct FileChooserDelegateClass : public ObjCClass <NSObject>
  20. {
  21. FileChooserDelegateClass() : ObjCClass <NSObject> ("JUCEFileChooser_")
  22. {
  23. addIvar<StringArray*> ("filters");
  24. addMethod (@selector (dealloc), dealloc, "v@:");
  25. addMethod (@selector (panel:shouldShowFilename:), shouldShowFilename, "c@:@@");
  26. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  27. addProtocol (@protocol (NSOpenSavePanelDelegate));
  28. #endif
  29. registerClass();
  30. }
  31. static void setFilters (id self, StringArray* filters)
  32. {
  33. object_setInstanceVariable (self, "filters", filters);
  34. }
  35. private:
  36. static void dealloc (id self, SEL)
  37. {
  38. delete getIvar<StringArray*> (self, "filters");
  39. sendSuperclassMessage (self, @selector (dealloc));
  40. }
  41. static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
  42. {
  43. StringArray* const filters = getIvar<StringArray*> (self, "filters");
  44. const File f (nsStringToJuce (filename));
  45. for (int i = filters->size(); --i >= 0;)
  46. if (f.getFileName().matchesWildcard ((*filters)[i], true))
  47. return true;
  48. #if (! defined (MAC_OS_X_VERSION_10_7)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
  49. NSError* error;
  50. NSString* name = [[NSWorkspace sharedWorkspace] typeOfFile: filename error: &error];
  51. if ([name isEqualToString: nsStringLiteral ("com.apple.alias-file")])
  52. {
  53. FSRef ref;
  54. FSPathMakeRef ((const UInt8*) [filename fileSystemRepresentation], &ref, nullptr);
  55. Boolean targetIsFolder = false, wasAliased = false;
  56. FSResolveAliasFileWithMountFlags (&ref, true, &targetIsFolder, &wasAliased, 0);
  57. return wasAliased && targetIsFolder;
  58. }
  59. #endif
  60. return f.isDirectory()
  61. && ! [[NSWorkspace sharedWorkspace] isFilePackageAtPath: filename];
  62. }
  63. };
  64. static NSMutableArray* createAllowedTypesArray (const StringArray& filters)
  65. {
  66. if (filters.size() == 0)
  67. return nil;
  68. NSMutableArray* filterArray = [[[NSMutableArray alloc] init] autorelease];
  69. for (int i = 0; i < filters.size(); ++i)
  70. {
  71. const String f (filters[i].replace ("*.", ""));
  72. if (f == "*")
  73. return nil;
  74. [filterArray addObject: juceStringToNS (f)];
  75. }
  76. return filterArray;
  77. }
  78. //==============================================================================
  79. void FileChooser::showPlatformDialog (Array<File>& results,
  80. const String& title,
  81. const File& currentFileOrDirectory,
  82. const String& filter,
  83. bool selectsDirectory,
  84. bool selectsFiles,
  85. bool isSaveDialogue,
  86. bool /*warnAboutOverwritingExistingFiles*/,
  87. bool selectMultipleFiles,
  88. FilePreviewComponent* /*extraInfoComponent*/)
  89. {
  90. JUCE_AUTORELEASEPOOL
  91. ScopedPointer<TemporaryMainMenuWithStandardCommands> tempMenu;
  92. if (JUCEApplication::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. bool FileChooser::isPlatformDialogAvailable()
  155. {
  156. return true;
  157. }
  158. #else
  159. //==============================================================================
  160. bool FileChooser::isPlatformDialogAvailable()
  161. {
  162. return false;
  163. }
  164. void FileChooser::showPlatformDialog (Array<File>& results,
  165. const String& title,
  166. const File& currentFileOrDirectory,
  167. const String& filter,
  168. bool selectsDirectory,
  169. bool selectsFiles,
  170. bool isSaveDialogue,
  171. bool warnAboutOverwritingExistingFiles,
  172. bool selectMultipleFiles,
  173. FilePreviewComponent* extraInfoComponent)
  174. {
  175. JUCE_AUTORELEASEPOOL
  176. jassertfalse; //there's no such thing in iOS
  177. }
  178. #endif