nfd_cocoa.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 )
  47. {
  48. if ( !defaultPath || strlen(defaultPath) == 0 )
  49. return;
  50. NSString *defaultPathString = [NSString stringWithUTF8String: defaultPath];
  51. NSURL *url = [NSURL fileURLWithPath:defaultPathString isDirectory:YES];
  52. [dialog setDirectoryURL:url];
  53. }
  54. /* fixme: pathset should be pathSet */
  55. static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset )
  56. {
  57. assert(pathset);
  58. assert([urls count]);
  59. pathset->count = (size_t)[urls count];
  60. pathset->indices = NFDi_Malloc( sizeof(size_t)*pathset->count );
  61. if ( !pathset->indices )
  62. {
  63. return NFD_ERROR;
  64. }
  65. // count the total space needed for buf
  66. size_t bufsize = 0;
  67. for ( NSURL *url in urls )
  68. {
  69. NSString *path = [url path];
  70. bufsize += [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  71. }
  72. pathset->buf = NFDi_Malloc( sizeof(nfdchar_t) * bufsize );
  73. if ( !pathset->buf )
  74. {
  75. return NFD_ERROR;
  76. }
  77. // fill buf
  78. nfdchar_t *p_buf = pathset->buf;
  79. size_t count = 0;
  80. for ( NSURL *url in urls )
  81. {
  82. NSString *path = [url path];
  83. const nfdchar_t *utf8Path = [path UTF8String];
  84. size_t byteLen = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  85. memcpy( p_buf, utf8Path, byteLen );
  86. ptrdiff_t index = p_buf - pathset->buf;
  87. assert( index >= 0 );
  88. pathset->indices[count] = (size_t)index;
  89. p_buf += byteLen;
  90. ++count;
  91. }
  92. return NFD_OKAY;
  93. }
  94. /* public */
  95. nfdresult_t NFD_OpenDialog( const char *filterList,
  96. const nfdchar_t *defaultPath,
  97. nfdchar_t **outPath )
  98. {
  99. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  100. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  101. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  102. [dialog setAllowsMultipleSelection:NO];
  103. // Build the filter list
  104. AddFilterListToDialog(dialog, filterList);
  105. // Set the starting directory
  106. SetDefaultPath(dialog, defaultPath);
  107. nfdresult_t nfdResult = NFD_CANCEL;
  108. if ( [dialog runModal] == NSModalResponseOK )
  109. {
  110. NSURL *url = [dialog URL];
  111. const char *utf8Path = [[url path] UTF8String];
  112. // byte count, not char count
  113. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  114. *outPath = NFDi_Malloc( len+1 );
  115. if ( !*outPath )
  116. {
  117. [pool release];
  118. return NFD_ERROR;
  119. }
  120. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  121. nfdResult = NFD_OKAY;
  122. }
  123. [pool release];
  124. [keyWindow makeKeyAndOrderFront:nil];
  125. return nfdResult;
  126. }
  127. nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
  128. const nfdchar_t *defaultPath,
  129. nfdpathset_t *outPaths )
  130. {
  131. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  132. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  133. [dialog setAllowsMultipleSelection:YES];
  134. // Build the fiter list.
  135. AddFilterListToDialog(dialog, filterList);
  136. // Set the starting directory
  137. SetDefaultPath(dialog, defaultPath);
  138. nfdresult_t nfdResult = NFD_CANCEL;
  139. if ( [dialog runModal] == NSModalResponseOK )
  140. {
  141. NSArray *urls = [dialog URLs];
  142. if ( [urls count] == 0 )
  143. {
  144. [pool release];
  145. return NFD_CANCEL;
  146. }
  147. if ( AllocPathSet( urls, outPaths ) == NFD_ERROR )
  148. {
  149. [pool release];
  150. return NFD_ERROR;
  151. }
  152. nfdResult = NFD_OKAY;
  153. }
  154. [pool release];
  155. return nfdResult;
  156. }
  157. nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
  158. const nfdchar_t *defaultPath,
  159. nfdchar_t **outPath )
  160. {
  161. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  162. NSSavePanel *dialog = [NSSavePanel savePanel];
  163. [dialog setExtensionHidden:NO];
  164. // Build the filter list.
  165. AddFilterListToDialog(dialog, filterList);
  166. // Set the starting directory
  167. SetDefaultPath(dialog, defaultPath);
  168. nfdresult_t nfdResult = NFD_CANCEL;
  169. if ( [dialog runModal] == NSModalResponseOK )
  170. {
  171. NSURL *url = [dialog URL];
  172. const char *utf8Path = [[url path] UTF8String];
  173. size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  174. *outPath = NFDi_Malloc( byteLen );
  175. if ( !*outPath )
  176. {
  177. [pool release];
  178. return NFD_ERROR;
  179. }
  180. memcpy( *outPath, utf8Path, byteLen );
  181. nfdResult = NFD_OKAY;
  182. }
  183. [pool release];
  184. return nfdResult;
  185. }
  186. nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath,
  187. nfdchar_t **outPath)
  188. {
  189. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  190. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  191. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  192. [dialog setAllowsMultipleSelection:NO];
  193. [dialog setCanChooseDirectories:YES];
  194. [dialog setCanCreateDirectories:YES];
  195. [dialog setCanChooseFiles:NO];
  196. // Set the starting directory
  197. SetDefaultPath(dialog, defaultPath);
  198. nfdresult_t nfdResult = NFD_CANCEL;
  199. if ( [dialog runModal] == NSModalResponseOK )
  200. {
  201. NSURL *url = [dialog URL];
  202. const char *utf8Path = [[url path] UTF8String];
  203. // byte count, not char count
  204. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  205. *outPath = NFDi_Malloc( len+1 );
  206. if ( !*outPath )
  207. {
  208. [pool release];
  209. return NFD_ERROR;
  210. }
  211. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  212. nfdResult = NFD_OKAY;
  213. }
  214. [pool release];
  215. [keyWindow makeKeyAndOrderFront:nil];
  216. return nfdResult;
  217. }