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.

251 lines
9.3KB

  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 ("JUCEFileChooser_")
  22. {
  23. addIvar<StringArray*> ("filters");
  24. addMethod (@selector (initWithFilters:), initWithFilters, "@@:^v");
  25. addMethod (@selector (dealloc), dealloc, "v@:");
  26. addMethod (@selector (panel:shouldShowFilename:), shouldShowFilename, "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. private:
  33. static id initWithFilters (id self, SEL, StringArray* filters)
  34. {
  35. self = sendSuperclassMessage (self, @selector (init));
  36. object_setInstanceVariable (self, "filters", filters);
  37. return self;
  38. }
  39. static void dealloc (id self, SEL)
  40. {
  41. delete getIvar<StringArray*> (self, "filters");
  42. sendSuperclassMessage (self, @selector (dealloc));
  43. }
  44. static BOOL shouldShowFilename (id self, SEL, id /*sender*/, NSString* filename)
  45. {
  46. StringArray* const filters = getIvar<StringArray*> (self, "filters");
  47. const File f (nsStringToJuce (filename));
  48. for (int i = filters->size(); --i >= 0;)
  49. if (f.getFileName().matchesWildcard ((*filters)[i], true))
  50. return true;
  51. #if (! defined (MAC_OS_X_VERSION_10_7)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
  52. NSError* error;
  53. NSString* name = [[NSWorkspace sharedWorkspace] typeOfFile: filename error: &error];
  54. if ([name isEqualToString: nsStringLiteral ("com.apple.alias-file")])
  55. {
  56. FSRef ref;
  57. FSPathMakeRef ((const UInt8*) [filename fileSystemRepresentation], &ref, nullptr);
  58. Boolean targetIsFolder = false, wasAliased = false;
  59. FSResolveAliasFileWithMountFlags (&ref, true, &targetIsFolder, &wasAliased, 0);
  60. return wasAliased && targetIsFolder;
  61. }
  62. #endif
  63. return f.isDirectory()
  64. && ! [[NSWorkspace sharedWorkspace] isFilePackageAtPath: filename];
  65. }
  66. };
  67. //==============================================================================
  68. class TemporaryMainMenuWithStandardCommands
  69. {
  70. public:
  71. TemporaryMainMenuWithStandardCommands()
  72. : oldMenu (MenuBarModel::getMacMainMenu())
  73. {
  74. MenuBarModel::setMacMainMenu (nullptr);
  75. NSMenu* menu = [[NSMenu alloc] initWithTitle: nsStringLiteral ("Edit")];
  76. NSMenuItem* item;
  77. item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (nsStringLiteral ("Cut"), nil)
  78. action: @selector (cut:) keyEquivalent: nsStringLiteral ("x")];
  79. [menu addItem: item];
  80. [item release];
  81. item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (nsStringLiteral ("Copy"), nil)
  82. action: @selector (copy:) keyEquivalent: nsStringLiteral ("c")];
  83. [menu addItem: item];
  84. [item release];
  85. item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (nsStringLiteral ("Paste"), nil)
  86. action: @selector (paste:) keyEquivalent: nsStringLiteral ("v")];
  87. [menu addItem: item];
  88. [item release];
  89. item = [[NSApp mainMenu] addItemWithTitle: NSLocalizedString (nsStringLiteral ("Edit"), nil)
  90. action: nil keyEquivalent: nsEmptyString()];
  91. [[NSApp mainMenu] setSubmenu: menu forItem: item];
  92. [menu release];
  93. }
  94. ~TemporaryMainMenuWithStandardCommands()
  95. {
  96. MenuBarModel::setMacMainMenu (oldMenu);
  97. }
  98. private:
  99. MenuBarModel* oldMenu;
  100. };
  101. //==============================================================================
  102. void FileChooser::showPlatformDialog (Array<File>& results,
  103. const String& title,
  104. const File& currentFileOrDirectory,
  105. const String& filter,
  106. bool selectsDirectory,
  107. bool selectsFiles,
  108. bool isSaveDialogue,
  109. bool /*warnAboutOverwritingExistingFiles*/,
  110. bool selectMultipleFiles,
  111. FilePreviewComponent* /*extraInfoComponent*/)
  112. {
  113. JUCE_AUTORELEASEPOOL
  114. const TemporaryMainMenuWithStandardCommands tempMenu;
  115. StringArray* filters = new StringArray();
  116. filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
  117. filters->trim();
  118. filters->removeEmptyStrings();
  119. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  120. typedef NSObject<NSOpenSavePanelDelegate> DelegateType;
  121. #else
  122. typedef NSObject DelegateType;
  123. #endif
  124. static FileChooserDelegateClass cls;
  125. DelegateType* delegate = [[cls.createInstance() performSelector: @selector (initWithFilters:)
  126. withObject: (id) filters] autorelease];
  127. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  128. : [NSOpenPanel openPanel];
  129. [panel setTitle: juceStringToNS (title)];
  130. if (! isSaveDialogue)
  131. {
  132. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  133. [openPanel setCanChooseDirectories: selectsDirectory];
  134. [openPanel setCanChooseFiles: selectsFiles];
  135. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  136. [openPanel setResolvesAliases: YES];
  137. }
  138. [panel setDelegate: delegate];
  139. if (isSaveDialogue || selectsDirectory)
  140. [panel setCanCreateDirectories: YES];
  141. String directory, filename;
  142. if (currentFileOrDirectory.isDirectory())
  143. {
  144. directory = currentFileOrDirectory.getFullPathName();
  145. }
  146. else
  147. {
  148. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  149. filename = currentFileOrDirectory.getFileName();
  150. }
  151. #if defined (MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6)
  152. [panel setDirectoryURL: [NSURL fileURLWithPath: juceStringToNS (directory)]];
  153. [panel setNameFieldStringValue: juceStringToNS (filename)];
  154. if ([panel runModal] == NSOKButton)
  155. #else
  156. if ([panel runModalForDirectory: juceStringToNS (directory)
  157. file: juceStringToNS (filename)] == NSOKButton)
  158. #endif
  159. {
  160. if (isSaveDialogue)
  161. {
  162. results.add (File (nsStringToJuce ([[panel URL] path])));
  163. }
  164. else
  165. {
  166. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  167. NSArray* urls = [openPanel URLs];
  168. for (unsigned int i = 0; i < [urls count]; ++i)
  169. results.add (File (nsStringToJuce ([[urls objectAtIndex: i] path])));
  170. }
  171. }
  172. [panel setDelegate: nil];
  173. }
  174. bool FileChooser::isPlatformDialogAvailable()
  175. {
  176. return true;
  177. }
  178. #else
  179. //==============================================================================
  180. bool FileChooser::isPlatformDialogAvailable()
  181. {
  182. return false;
  183. }
  184. void FileChooser::showPlatformDialog (Array<File>& results,
  185. const String& title,
  186. const File& currentFileOrDirectory,
  187. const String& filter,
  188. bool selectsDirectory,
  189. bool selectsFiles,
  190. bool isSaveDialogue,
  191. bool warnAboutOverwritingExistingFiles,
  192. bool selectMultipleFiles,
  193. FilePreviewComponent* extraInfoComponent)
  194. {
  195. JUCE_AUTORELEASEPOOL
  196. jassertfalse; //there's no such thing in iOS
  197. }
  198. #endif