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.

192 lines
6.6KB

  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. void FileChooser::showPlatformDialog (Array<File>& results,
  73. const String& title,
  74. const File& currentFileOrDirectory,
  75. const String& filter,
  76. bool selectsDirectory,
  77. bool selectsFiles,
  78. bool isSaveDialogue,
  79. bool /*warnAboutOverwritingExistingFiles*/,
  80. bool selectMultipleFiles,
  81. FilePreviewComponent* /*extraInfoComponent*/)
  82. {
  83. JUCE_AUTORELEASEPOOL
  84. StringArray* filters = new StringArray();
  85. filters->addTokens (filter.replaceCharacters (",:", ";;"), ";", String::empty);
  86. filters->trim();
  87. filters->removeEmptyStrings();
  88. JuceFileChooserDelegate* delegate = [[JuceFileChooserDelegate alloc] initWithFilters: filters];
  89. [delegate autorelease];
  90. NSSavePanel* panel = isSaveDialogue ? [NSSavePanel savePanel]
  91. : [NSOpenPanel openPanel];
  92. [panel setTitle: juceStringToNS (title)];
  93. if (! isSaveDialogue)
  94. {
  95. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  96. [openPanel setCanChooseDirectories: selectsDirectory];
  97. [openPanel setCanChooseFiles: selectsFiles];
  98. [openPanel setAllowsMultipleSelection: selectMultipleFiles];
  99. [openPanel setResolvesAliases: YES];
  100. }
  101. [panel setDelegate: delegate];
  102. if (isSaveDialogue || selectsDirectory)
  103. [panel setCanCreateDirectories: YES];
  104. String directory, filename;
  105. if (currentFileOrDirectory.isDirectory())
  106. {
  107. directory = currentFileOrDirectory.getFullPathName();
  108. }
  109. else
  110. {
  111. directory = currentFileOrDirectory.getParentDirectory().getFullPathName();
  112. filename = currentFileOrDirectory.getFileName();
  113. }
  114. if ([panel runModalForDirectory: juceStringToNS (directory)
  115. file: juceStringToNS (filename)]
  116. == NSOKButton)
  117. {
  118. if (isSaveDialogue)
  119. {
  120. results.add (File (nsStringToJuce ([panel filename])));
  121. }
  122. else
  123. {
  124. NSOpenPanel* openPanel = (NSOpenPanel*) panel;
  125. NSArray* urls = [openPanel filenames];
  126. for (unsigned int i = 0; i < [urls count]; ++i)
  127. {
  128. NSString* f = [urls objectAtIndex: i];
  129. results.add (File (nsStringToJuce (f)));
  130. }
  131. }
  132. }
  133. [panel setDelegate: nil];
  134. }
  135. #else
  136. //==============================================================================
  137. void FileChooser::showPlatformDialog (Array<File>& results,
  138. const String& title,
  139. const File& currentFileOrDirectory,
  140. const String& filter,
  141. bool selectsDirectory,
  142. bool selectsFiles,
  143. bool isSaveDialogue,
  144. bool warnAboutOverwritingExistingFiles,
  145. bool selectMultipleFiles,
  146. FilePreviewComponent* extraInfoComponent)
  147. {
  148. JUCE_AUTORELEASEPOOL
  149. jassertfalse; //xxx to do
  150. }
  151. #endif