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.

286 lines
11KB

  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. //==============================================================================
  65. class TemporaryMainMenuWithStandardCommands
  66. {
  67. public:
  68. TemporaryMainMenuWithStandardCommands()
  69. : oldMenu (MenuBarModel::getMacMainMenu()), oldAppleMenu (nullptr)
  70. {
  71. if (const PopupMenu* appleMenu = MenuBarModel::getMacExtraAppleItemsMenu())
  72. oldAppleMenu = new PopupMenu (*appleMenu);
  73. MenuBarModel::setMacMainMenu (nullptr);
  74. NSMenu* menu = [[NSMenu alloc] initWithTitle: nsStringLiteral ("Edit")];
  75. NSMenuItem* item;
  76. item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (nsStringLiteral ("Cut"), nil)
  77. action: @selector (cut:) keyEquivalent: nsStringLiteral ("x")];
  78. [menu addItem: item];
  79. [item release];
  80. item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (nsStringLiteral ("Copy"), nil)
  81. action: @selector (copy:) keyEquivalent: nsStringLiteral ("c")];
  82. [menu addItem: item];
  83. [item release];
  84. item = [[NSMenuItem alloc] initWithTitle: NSLocalizedString (nsStringLiteral ("Paste"), nil)
  85. action: @selector (paste:) keyEquivalent: nsStringLiteral ("v")];
  86. [menu addItem: item];
  87. [item release];
  88. item = [[NSApp mainMenu] addItemWithTitle: NSLocalizedString (nsStringLiteral ("Edit"), nil)
  89. action: nil keyEquivalent: nsEmptyString()];
  90. [[NSApp mainMenu] setSubmenu: menu forItem: item];
  91. [menu release];
  92. // use a dummy modal component so that apps can tell that something is currently modal.
  93. dummyModalComponent.enterModalState();
  94. }
  95. ~TemporaryMainMenuWithStandardCommands()
  96. {
  97. MenuBarModel::setMacMainMenu (oldMenu, oldAppleMenu);
  98. }
  99. private:
  100. MenuBarModel* oldMenu;
  101. ScopedPointer<PopupMenu> oldAppleMenu;
  102. // The OS view already plays an alert when clicking outside
  103. // the modal comp, so this override avoids adding extra
  104. // inappropriate noises when the cancel button is pressed.
  105. // This override is also important because it stops the base class
  106. // calling ModalComponentManager::bringToFront, which can get
  107. // recursive when file dialogs are involved
  108. class SilentDummyModalComp : public Component
  109. {
  110. public:
  111. SilentDummyModalComp() {}
  112. void inputAttemptWhenModal() {}
  113. };
  114. SilentDummyModalComp dummyModalComponent;
  115. };
  116. static NSMutableArray* createAllowedTypesArray (const StringArray& filters)
  117. {
  118. NSMutableArray* filterArray = [[[NSMutableArray alloc] init] autorelease];
  119. for (int i = 0; i < filters.size(); ++i)
  120. [filterArray addObject: juceStringToNS (filters[i].replace ("*.", ""))];
  121. if (filters.size() == 0)
  122. [filterArray addObject: juceStringToNS ("*")];
  123. return filterArray;
  124. }
  125. //==============================================================================
  126. void FileChooser::showPlatformDialog (Array<File>& results,
  127. const String& title,
  128. const File& currentFileOrDirectory,
  129. const String& filter,
  130. bool selectsDirectory,
  131. bool selectsFiles,
  132. bool isSaveDialogue,
  133. bool /*warnAboutOverwritingExistingFiles*/,
  134. bool selectMultipleFiles,
  135. FilePreviewComponent* /*extraInfoComponent*/)
  136. {
  137. JUCE_AUTORELEASEPOOL
  138. ScopedPointer<TemporaryMainMenuWithStandardCommands> tempMenu;
  139. if (JUCEApplication::isStandaloneApp())
  140. tempMenu = new TemporaryMainMenuWithStandardCommands();
  141. StringArray* filters = new StringArray();
  142. filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
  143. filters->trim();
  144. filters->removeEmptyStrings();
  145. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  146. typedef NSObject<NSOpenSavePanelDelegate> DelegateType;
  147. #else
  148. typedef NSObject DelegateType;
  149. #endif
  150. static FileChooserDelegateClass cls;
  151. DelegateType* delegate = (DelegateType*) [[cls.createInstance() init] autorelease];
  152. FileChooserDelegateClass::setFilters (delegate, filters);
  153. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  154. : [NSOpenPanel openPanel];
  155. [panel setTitle: juceStringToNS (title)];
  156. [panel setAllowedFileTypes: createAllowedTypesArray (*filters)];
  157. if (! isSaveDialogue)
  158. {
  159. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  160. [openPanel setCanChooseDirectories: selectsDirectory];
  161. [openPanel setCanChooseFiles: selectsFiles];
  162. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  163. [openPanel setResolvesAliases: YES];
  164. }
  165. [panel setDelegate: delegate];
  166. if (isSaveDialogue || selectsDirectory)
  167. [panel setCanCreateDirectories: YES];
  168. String directory, filename;
  169. if (currentFileOrDirectory.isDirectory())
  170. {
  171. directory = currentFileOrDirectory.getFullPathName();
  172. }
  173. else
  174. {
  175. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  176. filename = currentFileOrDirectory.getFileName();
  177. }
  178. #if defined (MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6)
  179. [panel setDirectoryURL: [NSURL fileURLWithPath: juceStringToNS (directory)]];
  180. [panel setNameFieldStringValue: juceStringToNS (filename)];
  181. if ([panel runModal] == NSOKButton)
  182. #else
  183. if ([panel runModalForDirectory: juceStringToNS (directory)
  184. file: juceStringToNS (filename)] == NSOKButton)
  185. #endif
  186. {
  187. if (isSaveDialogue)
  188. {
  189. results.add (File (nsStringToJuce ([[panel URL] path])));
  190. }
  191. else
  192. {
  193. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  194. NSArray* urls = [openPanel URLs];
  195. for (unsigned int i = 0; i < [urls count]; ++i)
  196. results.add (File (nsStringToJuce ([[urls objectAtIndex: i] path])));
  197. }
  198. }
  199. [panel setDelegate: nil];
  200. }
  201. bool FileChooser::isPlatformDialogAvailable()
  202. {
  203. return true;
  204. }
  205. #else
  206. //==============================================================================
  207. bool FileChooser::isPlatformDialogAvailable()
  208. {
  209. return false;
  210. }
  211. void FileChooser::showPlatformDialog (Array<File>& results,
  212. const String& title,
  213. const File& currentFileOrDirectory,
  214. const String& filter,
  215. bool selectsDirectory,
  216. bool selectsFiles,
  217. bool isSaveDialogue,
  218. bool warnAboutOverwritingExistingFiles,
  219. bool selectMultipleFiles,
  220. FilePreviewComponent* extraInfoComponent)
  221. {
  222. JUCE_AUTORELEASEPOOL
  223. jassertfalse; //there's no such thing in iOS
  224. }
  225. #endif