BsMacOSBrowseDialogs.mm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #import <AppKit/AppKit.h>
  4. #include "Utility/BsEditorUtility.h"
  5. namespace bs
  6. {
  7. bool EditorUtility::openBrowseDialog(FileDialogType type, const Path& defaultPath, const String& filterList,
  8. Vector<Path>& paths)
  9. {
  10. Vector<String> extensions;
  11. if(!filterList.empty())
  12. {
  13. extensions = StringUtil::split(filterList, ";");
  14. for(auto& entry : extensions)
  15. {
  16. String output;
  17. output.reserve(entry.size());
  18. bool foundLeadingDot = false;
  19. for(auto& curChar : entry)
  20. {
  21. // Skip leading dot
  22. if(curChar == '.' && !foundLeadingDot)
  23. {
  24. foundLeadingDot = true;
  25. continue;
  26. }
  27. // Skip wildcard character
  28. if(curChar == '*')
  29. continue;
  30. output += curChar;
  31. }
  32. entry = output;
  33. }
  34. }
  35. @autoreleasepool
  36. {
  37. bool save = ((UINT32) type & (UINT32) FileDialogType::Save) != 0;
  38. String path = defaultPath.toString();
  39. if (!save)
  40. {
  41. bool file = ((UINT32) type & (UINT32) FileDialogType::OpenFile) != 0;
  42. bool folder = ((UINT32) type & (UINT32) FileDialogType::OpenFolder) != 0;
  43. bool multiselect = ((UINT32) type & (UINT32) FileDialogType::Multiselect) != 0;
  44. NSOpenPanel* openDlg = [NSOpenPanel openPanel];
  45. [openDlg setCanChooseFiles:file];
  46. [openDlg setCanChooseDirectories:folder];
  47. [openDlg setAllowsMultipleSelection:multiselect];
  48. [openDlg setCanCreateDirectories:YES];
  49. NSString* pathString = [[NSString stringWithUTF8String:path.c_str()] stringByResolvingSymlinksInPath];
  50. [openDlg setDirectoryURL:[NSURL fileURLWithPath:pathString]];
  51. NSMutableArray* fileTypes = [[NSMutableArray alloc] init];
  52. for (UINT32 i = 0; i < (UINT32) extensions.size(); i++)
  53. {
  54. NSString* extensionString = [NSString stringWithUTF8String:extensions[i].c_str()];
  55. [fileTypes addObject:extensionString];
  56. }
  57. if(fileTypes.count > 0)
  58. [openDlg setAllowedFileTypes:fileTypes];
  59. if ([openDlg runModal] == NSModalResponseOK)
  60. {
  61. NSArray* files = [openDlg URLs];
  62. for (NSURL* file in files)
  63. {
  64. String fileStr = String([[file path] cStringUsingEncoding:kCFStringEncodingUTF8]);
  65. paths.push_back(fileStr);
  66. }
  67. return true;
  68. }
  69. }
  70. else
  71. {
  72. NSSavePanel* saveDlg = [NSSavePanel savePanel];
  73. [saveDlg setCanCreateDirectories:YES];
  74. NSString* pathString = [[NSString stringWithUTF8String:path.c_str()] stringByResolvingSymlinksInPath];
  75. [saveDlg setDirectoryURL:[NSURL fileURLWithPath:pathString]];
  76. NSMutableArray* fileTypes = [[NSMutableArray alloc] init];
  77. for(UINT32 i = 0; i < (UINT32)extensions.size(); i++)
  78. {
  79. NSString* extensionString = [NSString stringWithUTF8String:extensions[i].c_str()];
  80. [fileTypes addObject:extensionString];
  81. }
  82. if(fileTypes.count > 0)
  83. [saveDlg setAllowedFileTypes:fileTypes];
  84. if([saveDlg runModal] == NSModalResponseOK)
  85. {
  86. NSURL* file = [saveDlg URL];
  87. String fileStr = String([[file path] cStringUsingEncoding:kCFStringEncodingUTF8]);
  88. paths.push_back(fileStr);
  89. return true;
  90. }
  91. return false;
  92. }
  93. return false;
  94. }
  95. }
  96. }