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.

173 lines
5.9KB

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