nfd_cocoa.mm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Native File Dialog
  3. http://www.frogtoss.com/labs
  4. */
  5. #include <AppKit/AppKit.h>
  6. #include "nfd.h"
  7. #include "nfd_common.h"
  8. static NSArray *BuildAllowedFileTypes( const char *filterList )
  9. {
  10. // Commas and semicolons are the same thing on this platform
  11. NSMutableArray *buildFilterList = [[NSMutableArray alloc] init];
  12. char typebuf[NFD_MAX_STRLEN] = {0};
  13. size_t filterListLen = strlen(filterList);
  14. char *p_typebuf = typebuf;
  15. for ( size_t i = 0; i < filterListLen+1; ++i )
  16. {
  17. if ( filterList[i] == ',' || filterList[i] == ';' || filterList[i] == '\0' )
  18. {
  19. ++p_typebuf;
  20. *p_typebuf = '\0';
  21. NSString *thisType = [NSString stringWithUTF8String: typebuf];
  22. [buildFilterList addObject:thisType];
  23. p_typebuf = typebuf;
  24. *p_typebuf = '\0';
  25. }
  26. else
  27. {
  28. *p_typebuf = filterList[i];
  29. ++p_typebuf;
  30. }
  31. }
  32. NSArray *returnArray = [NSArray arrayWithArray:buildFilterList];
  33. [buildFilterList release];
  34. return returnArray;
  35. }
  36. static void AddFilterListToDialog( NSSavePanel *dialog, const char *filterList )
  37. {
  38. if ( !filterList || strlen(filterList) == 0 )
  39. return;
  40. NSArray *allowedFileTypes = BuildAllowedFileTypes( filterList );
  41. if ( [allowedFileTypes count] != 0 )
  42. {
  43. [dialog setAllowedFileTypes:allowedFileTypes];
  44. }
  45. }
  46. static void SetDefaultPath( NSSavePanel *dialog, const nfdchar_t *defaultPath, const nfdchar_t *defaultFilename )
  47. {
  48. if ( defaultPath && strlen(defaultPath))
  49. {
  50. NSString *defaultPathString = [NSString stringWithUTF8String: defaultPath];
  51. NSURL *url = [NSURL fileURLWithPath:defaultPathString isDirectory:YES];
  52. [dialog setDirectoryURL:url];
  53. }
  54. if ( defaultFilename && strlen(defaultFilename))
  55. {
  56. NSString *defaultFilenameString = [NSString stringWithUTF8String: defaultFilename];
  57. [dialog setNameFieldStringValue:defaultFilenameString];
  58. }
  59. }
  60. /* fixme: pathset should be pathSet */
  61. static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset )
  62. {
  63. assert(pathset);
  64. assert([urls count]);
  65. pathset->count = (size_t)[urls count];
  66. pathset->indices = (size_t *) NFDi_Malloc( sizeof(size_t)*pathset->count );
  67. if ( !pathset->indices )
  68. {
  69. return NFD_ERROR;
  70. }
  71. // count the total space needed for buf
  72. size_t bufsize = 0;
  73. for ( NSURL *url in urls )
  74. {
  75. NSString *path = [url path];
  76. bufsize += [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  77. }
  78. pathset->buf = (nfdchar_t*) NFDi_Malloc( sizeof(nfdchar_t) * bufsize );
  79. if ( !pathset->buf )
  80. {
  81. return NFD_ERROR;
  82. }
  83. // fill buf
  84. nfdchar_t *p_buf = pathset->buf;
  85. size_t count = 0;
  86. for ( NSURL *url in urls )
  87. {
  88. NSString *path = [url path];
  89. const nfdchar_t *utf8Path = [path UTF8String];
  90. size_t byteLen = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  91. memcpy( p_buf, utf8Path, byteLen );
  92. ptrdiff_t index = p_buf - pathset->buf;
  93. assert( index >= 0 );
  94. pathset->indices[count] = (size_t)index;
  95. p_buf += byteLen;
  96. ++count;
  97. }
  98. return NFD_OKAY;
  99. }
  100. /* public */
  101. nfdresult_t NFD_OpenDialog( const char *filterList,
  102. const nfdchar_t *defaultPath,
  103. nfdchar_t **outPath )
  104. {
  105. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  106. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  107. [dialog setAllowsMultipleSelection:NO];
  108. // Build the filter list
  109. AddFilterListToDialog(dialog, filterList);
  110. // Set the starting directory
  111. SetDefaultPath(dialog, defaultPath, NULL);
  112. nfdresult_t nfdResult = NFD_CANCEL;
  113. if ( [dialog runModal] == NSModalResponseOK )
  114. {
  115. NSURL *url = [dialog URL];
  116. const char *utf8Path = [[url path] UTF8String];
  117. // byte count, not char count
  118. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  119. *outPath = (nfdchar_t*) NFDi_Malloc( len+1 );
  120. if ( !*outPath )
  121. {
  122. [pool release];
  123. return NFD_ERROR;
  124. }
  125. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  126. nfdResult = NFD_OKAY;
  127. }
  128. [pool release];
  129. return nfdResult;
  130. }
  131. nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
  132. const nfdchar_t *defaultPath,
  133. nfdpathset_t *outPaths )
  134. {
  135. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  136. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  137. [dialog setAllowsMultipleSelection:YES];
  138. // Build the fiter list.
  139. AddFilterListToDialog(dialog, filterList);
  140. // Set the starting directory
  141. SetDefaultPath(dialog, defaultPath, NULL);
  142. nfdresult_t nfdResult = NFD_CANCEL;
  143. if ( [dialog runModal] == NSModalResponseOK )
  144. {
  145. NSArray *urls = [dialog URLs];
  146. if ( [urls count] == 0 )
  147. {
  148. [pool release];
  149. return NFD_CANCEL;
  150. }
  151. if ( AllocPathSet( urls, outPaths ) == NFD_ERROR )
  152. {
  153. [pool release];
  154. return NFD_ERROR;
  155. }
  156. nfdResult = NFD_OKAY;
  157. }
  158. [pool release];
  159. return nfdResult;
  160. }
  161. nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
  162. const nfdchar_t *defaultPath,
  163. const nfdchar_t *defaultFilename,
  164. nfdchar_t **outPath )
  165. {
  166. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  167. NSSavePanel *dialog = [NSSavePanel savePanel];
  168. [dialog setExtensionHidden:NO];
  169. [dialog setCanCreateDirectories:YES];
  170. // Build the filter list.
  171. AddFilterListToDialog(dialog, filterList);
  172. // Set the starting directory
  173. SetDefaultPath(dialog, defaultPath, defaultFilename);
  174. nfdresult_t nfdResult = NFD_CANCEL;
  175. if ( [dialog runModal] == NSModalResponseOK )
  176. {
  177. NSURL *url = [dialog URL];
  178. const char *utf8Path = [[url path] UTF8String];
  179. size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  180. *outPath = (nfdchar_t*) NFDi_Malloc( byteLen );
  181. if ( !*outPath )
  182. {
  183. [pool release];
  184. return NFD_ERROR;
  185. }
  186. memcpy( *outPath, utf8Path, byteLen );
  187. nfdResult = NFD_OKAY;
  188. }
  189. [pool release];
  190. return nfdResult;
  191. }
  192. nfdresult_t NFD_ChooseDirectory(const nfdchar_t *prompt, const nfdchar_t *defaultPath,
  193. nfdchar_t **outPath )
  194. {
  195. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  196. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  197. [dialog setAllowsMultipleSelection:NO];
  198. [dialog setCanChooseDirectories:YES];
  199. [dialog setCanCreateDirectories:YES];
  200. [dialog setTitle:[NSString stringWithUTF8String: prompt]];
  201. [dialog setPrompt:@"Ok"];
  202. // Set the starting directory
  203. SetDefaultPath(dialog, defaultPath, NULL);
  204. nfdresult_t nfdResult = NFD_CANCEL;
  205. if ( [dialog runModal] == NSModalResponseOK )
  206. {
  207. NSURL *url = [dialog URL];
  208. const char *utf8Path = [[url path] UTF8String];
  209. // byte count, not char count
  210. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  211. *outPath = (nfdchar_t*) NFDi_Malloc( len+1 );
  212. if ( !*outPath )
  213. {
  214. [pool release];
  215. return NFD_ERROR;
  216. }
  217. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  218. nfdResult = NFD_OKAY;
  219. }
  220. [pool release];
  221. return nfdResult;
  222. }