nfd_cocoa.mm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  107. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  108. [dialog setAllowsMultipleSelection:NO];
  109. // Build the filter list
  110. AddFilterListToDialog(dialog, filterList);
  111. // Set the starting directory
  112. SetDefaultPath(dialog, defaultPath, NULL);
  113. nfdresult_t nfdResult = NFD_CANCEL;
  114. if ( [dialog runModal] == NSModalResponseOK )
  115. {
  116. NSURL *url = [dialog URL];
  117. const char *utf8Path = [[url path] UTF8String];
  118. // byte count, not char count
  119. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  120. *outPath = (nfdchar_t*) NFDi_Malloc( len+1 );
  121. if ( !*outPath )
  122. {
  123. [pool release];
  124. return NFD_ERROR;
  125. }
  126. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  127. nfdResult = NFD_OKAY;
  128. }
  129. [keyWindow makeKeyAndOrderFront:nil];
  130. [pool release];
  131. return nfdResult;
  132. }
  133. nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
  134. const nfdchar_t *defaultPath,
  135. nfdpathset_t *outPaths )
  136. {
  137. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  138. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  139. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  140. [dialog setAllowsMultipleSelection:YES];
  141. // Build the fiter list.
  142. AddFilterListToDialog(dialog, filterList);
  143. // Set the starting directory
  144. SetDefaultPath(dialog, defaultPath, NULL);
  145. nfdresult_t nfdResult = NFD_CANCEL;
  146. if ( [dialog runModal] == NSModalResponseOK )
  147. {
  148. NSArray *urls = [dialog URLs];
  149. if ( [urls count] == 0 )
  150. {
  151. [pool release];
  152. return NFD_CANCEL;
  153. }
  154. if ( AllocPathSet( urls, outPaths ) == NFD_ERROR )
  155. {
  156. [pool release];
  157. return NFD_ERROR;
  158. }
  159. nfdResult = NFD_OKAY;
  160. }
  161. [keyWindow makeKeyAndOrderFront:nil];
  162. [pool release];
  163. return nfdResult;
  164. }
  165. nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
  166. const nfdchar_t *defaultPath,
  167. const nfdchar_t *defaultFilename,
  168. nfdchar_t **outPath )
  169. {
  170. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  171. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  172. NSSavePanel *dialog = [NSSavePanel savePanel];
  173. [dialog setExtensionHidden:NO];
  174. [dialog setCanCreateDirectories:YES];
  175. // Build the filter list.
  176. AddFilterListToDialog(dialog, filterList);
  177. // Set the starting directory
  178. SetDefaultPath(dialog, defaultPath, defaultFilename);
  179. nfdresult_t nfdResult = NFD_CANCEL;
  180. if ( [dialog runModal] == NSModalResponseOK )
  181. {
  182. NSURL *url = [dialog URL];
  183. const char *utf8Path = [[url path] UTF8String];
  184. size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  185. *outPath = (nfdchar_t*) NFDi_Malloc( byteLen );
  186. if ( !*outPath )
  187. {
  188. [pool release];
  189. return NFD_ERROR;
  190. }
  191. memcpy( *outPath, utf8Path, byteLen );
  192. nfdResult = NFD_OKAY;
  193. }
  194. [keyWindow makeKeyAndOrderFront:nil];
  195. [pool release];
  196. return nfdResult;
  197. }
  198. nfdresult_t NFD_ChooseDirectory(const nfdchar_t *prompt, const nfdchar_t *defaultPath,
  199. nfdchar_t **outPath )
  200. {
  201. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  202. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  203. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  204. [dialog setAllowsMultipleSelection:NO];
  205. [dialog setCanChooseDirectories:YES];
  206. [dialog setCanCreateDirectories:YES];
  207. [dialog setTitle:[NSString stringWithUTF8String: prompt]];
  208. [dialog setPrompt:@"Ok"];
  209. // Set the starting directory
  210. SetDefaultPath(dialog, defaultPath, NULL);
  211. nfdresult_t nfdResult = NFD_CANCEL;
  212. if ( [dialog runModal] == NSModalResponseOK )
  213. {
  214. NSURL *url = [dialog URL];
  215. const char *utf8Path = [[url path] UTF8String];
  216. // byte count, not char count
  217. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  218. *outPath = (nfdchar_t*) NFDi_Malloc( len+1 );
  219. if ( !*outPath )
  220. {
  221. [pool release];
  222. return NFD_ERROR;
  223. }
  224. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  225. nfdResult = NFD_OKAY;
  226. }
  227. [keyWindow makeKeyAndOrderFront:nil];
  228. [pool release];
  229. return nfdResult;
  230. }