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.

250 lines
8.5KB

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