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.

202 lines
6.7KB

  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. //==============================================================================
  20. END_JUCE_NAMESPACE
  21. using namespace juce;
  22. #define JuceFileChooserDelegate MakeObjCClassName(JuceFileChooserDelegate)
  23. #if defined (MAC_OS_X_VERSION_10_6) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
  24. @interface JuceFileChooserDelegate : NSObject <NSOpenSavePanelDelegate>
  25. #else
  26. @interface JuceFileChooserDelegate : NSObject
  27. #endif
  28. {
  29. StringArray* filters;
  30. }
  31. - (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_;
  32. - (void) dealloc;
  33. - (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename;
  34. @end
  35. @implementation JuceFileChooserDelegate
  36. - (JuceFileChooserDelegate*) initWithFilters: (StringArray*) filters_
  37. {
  38. [super init];
  39. filters = filters_;
  40. return self;
  41. }
  42. - (void) dealloc
  43. {
  44. delete filters;
  45. [super dealloc];
  46. }
  47. - (BOOL) panel: (id) sender shouldShowFilename: (NSString*) filename
  48. {
  49. (void) sender;
  50. const File f (nsStringToJuce (filename));
  51. for (int i = filters->size(); --i >= 0;)
  52. if (f.getFileName().matchesWildcard ((*filters)[i], true))
  53. return true;
  54. #if (! defined (MAC_OS_X_VERSION_10_7)) || MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_7
  55. NSError* error;
  56. NSString* name = [[NSWorkspace sharedWorkspace] typeOfFile: filename error: &error];
  57. if ([name isEqualToString: nsStringLiteral ("com.apple.alias-file")])
  58. {
  59. FSRef ref;
  60. FSPathMakeRef ((const UInt8*) [filename fileSystemRepresentation], &ref, nullptr);
  61. Boolean targetIsFolder = false, wasAliased = false;
  62. FSResolveAliasFileWithMountFlags (&ref, true, &targetIsFolder, &wasAliased, 0);
  63. return wasAliased && targetIsFolder;
  64. }
  65. #endif
  66. return f.isDirectory()
  67. && ! [[NSWorkspace sharedWorkspace] isFilePackageAtPath: filename];
  68. }
  69. @end
  70. BEGIN_JUCE_NAMESPACE
  71. //==============================================================================
  72. bool FileChooser::isPlatformDialogAvailable()
  73. {
  74. return true;
  75. }
  76. void FileChooser::showPlatformDialog (Array<File>& results,
  77. const String& title,
  78. const File& currentFileOrDirectory,
  79. const String& filter,
  80. bool selectsDirectory,
  81. bool selectsFiles,
  82. bool isSaveDialogue,
  83. bool /*warnAboutOverwritingExistingFiles*/,
  84. bool selectMultipleFiles,
  85. FilePreviewComponent* /*extraInfoComponent*/)
  86. {
  87. JUCE_AUTORELEASEPOOL
  88. StringArray* filters = new StringArray();
  89. filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
  90. filters->trim();
  91. filters->removeEmptyStrings();
  92. JuceFileChooserDelegate* delegate = [[JuceFileChooserDelegate alloc] initWithFilters: filters];
  93. [delegate autorelease];
  94. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  95. : [NSOpenPanel openPanel];
  96. [panel setTitle: juceStringToNS (title)];
  97. if (! isSaveDialogue)
  98. {
  99. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  100. [openPanel setCanChooseDirectories: selectsDirectory];
  101. [openPanel setCanChooseFiles: selectsFiles];
  102. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  103. [openPanel setResolvesAliases: YES];
  104. }
  105. [panel setDelegate: delegate];
  106. if (isSaveDialogue || selectsDirectory)
  107. [panel setCanCreateDirectories: YES];
  108. String directory, filename;
  109. if (currentFileOrDirectory.isDirectory())
  110. {
  111. directory = currentFileOrDirectory.getFullPathName();
  112. }
  113. else
  114. {
  115. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  116. filename = currentFileOrDirectory.getFileName();
  117. }
  118. if ([panel runModalForDirectory: juceStringToNS (directory)
  119. file: juceStringToNS (filename)]
  120. == NSOKButton)
  121. {
  122. if (isSaveDialogue)
  123. {
  124. results.add (File (nsStringToJuce ([panel filename])));
  125. }
  126. else
  127. {
  128. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  129. NSArray* urls = [openPanel filenames];
  130. for (unsigned int i = 0; i < [urls count]; ++i)
  131. {
  132. NSString* f = [urls objectAtIndex: i];
  133. results.add (File (nsStringToJuce (f)));
  134. }
  135. }
  136. }
  137. [panel setDelegate: nil];
  138. }
  139. #else
  140. //==============================================================================
  141. bool FileChooser::isPlatformDialogAvailable()
  142. {
  143. return false;
  144. }
  145. void FileChooser::showPlatformDialog (Array<File>& results,
  146. const String& title,
  147. const File& currentFileOrDirectory,
  148. const String& filter,
  149. bool selectsDirectory,
  150. bool selectsFiles,
  151. bool isSaveDialogue,
  152. bool warnAboutOverwritingExistingFiles,
  153. bool selectMultipleFiles,
  154. FilePreviewComponent* extraInfoComponent)
  155. {
  156. JUCE_AUTORELEASEPOOL
  157. jassertfalse; //xxx to do
  158. }
  159. #endif