nfd.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // This software is provided 'as-is', without any express or implied
  2. // warranty. In no event will the authors be held liable for any damages
  3. // arising from the use of this software.
  4. // Permission is granted to anyone to use this software for any purpose,
  5. // including commercial applications, and to alter it and redistribute it
  6. // freely, subject to the following restrictions:
  7. // 1. The origin of this software must not be misrepresented; you must not
  8. // claim that you wrote the original software. If you use this software
  9. // in a product, an acknowledgment in the product documentation would be
  10. // appreciated but is not required.
  11. // 2. Altered source versions must be plainly marked as such, and must not be
  12. // misrepresented as being the original software.
  13. // 3. This notice may not be removed or altered from any source distribution.
  14. // https://github.com/mlabbe/nativefiledialog
  15. #include <AppKit/AppKit.h>
  16. #include "nfd.h"
  17. static NSArray *BuildAllowedFileTypes( const char *filterList ) {
  18. // Commas and semicolons are the same thing on this platform
  19. NSMutableArray *buildFilterList = [[NSMutableArray alloc] init];
  20. char typebuf[NFD_MAX_STRLEN] = {0};
  21. size_t filterListLen = strlen(filterList);
  22. char *p_typebuf = typebuf;
  23. for ( size_t i = 0; i < filterListLen+1; ++i )
  24. {
  25. if ( filterList[i] == ',' || filterList[i] == ';' || filterList[i] == '\0' )
  26. {
  27. if (filterList[i] != '\0')
  28. ++p_typebuf;
  29. *p_typebuf = '\0';
  30. NSString *thisType = [NSString stringWithUTF8String: typebuf];
  31. [buildFilterList addObject:thisType];
  32. p_typebuf = typebuf;
  33. *p_typebuf = '\0';
  34. }
  35. else
  36. {
  37. *p_typebuf = filterList[i];
  38. ++p_typebuf;
  39. }
  40. }
  41. NSArray *returnArray = [NSArray arrayWithArray:buildFilterList];
  42. // [buildFilterList release];
  43. return returnArray;
  44. }
  45. static void AddFilterListToDialog( NSSavePanel *dialog, const char *filterList ) {
  46. if ( !filterList || strlen(filterList) == 0 )
  47. return;
  48. NSArray *allowedFileTypes = BuildAllowedFileTypes( filterList );
  49. if ( [allowedFileTypes count] != 0 )
  50. {
  51. [dialog setAllowedFileTypes:allowedFileTypes];
  52. }
  53. }
  54. static void SetDefaultPath( NSSavePanel *dialog, const nfdchar_t *defaultPath ) {
  55. if ( !defaultPath || strlen(defaultPath) == 0 )
  56. return;
  57. NSString *defaultPathString = [NSString stringWithUTF8String: defaultPath];
  58. NSURL *url = [NSURL fileURLWithPath:defaultPathString isDirectory:YES];
  59. [dialog setDirectoryURL:url];
  60. }
  61. /* fixme: pathset should be pathSet */
  62. static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset ) {
  63. assert(pathset);
  64. assert([urls count]);
  65. pathset->count = (size_t)[urls count];
  66. pathset->indices = 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 = 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. nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList,
  101. const nfdchar_t *defaultPath,
  102. nfdchar_t **outPath ) {
  103. // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  104. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  105. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  106. [dialog setAllowsMultipleSelection:NO];
  107. // Build the filter list
  108. AddFilterListToDialog(dialog, filterList);
  109. // Set the starting directory
  110. SetDefaultPath(dialog, defaultPath);
  111. nfdresult_t nfdResult = NFD_CANCEL;
  112. if ( [dialog runModal] == NSModalResponseOK )
  113. {
  114. NSURL *url = [dialog URL];
  115. const char *utf8Path = [[url path] UTF8String];
  116. // byte count, not char count
  117. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  118. *outPath = NFDi_Malloc( len+1 );
  119. if ( !*outPath )
  120. {
  121. // [pool release];
  122. [keyWindow makeKeyAndOrderFront:nil];
  123. return NFD_ERROR;
  124. }
  125. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  126. nfdResult = NFD_OKAY;
  127. }
  128. // [pool release];
  129. [keyWindow makeKeyAndOrderFront:nil];
  130. return nfdResult;
  131. }
  132. nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
  133. const nfdchar_t *defaultPath,
  134. nfdpathset_t *outPaths ) {
  135. // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  136. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  137. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  138. [dialog setAllowsMultipleSelection:YES];
  139. // Build the fiter list.
  140. AddFilterListToDialog(dialog, filterList);
  141. // Set the starting directory
  142. SetDefaultPath(dialog, defaultPath);
  143. nfdresult_t nfdResult = NFD_CANCEL;
  144. if ( [dialog runModal] == NSModalResponseOK )
  145. {
  146. NSArray *urls = [dialog URLs];
  147. if ( [urls count] == 0 )
  148. {
  149. // [pool release];
  150. [keyWindow makeKeyAndOrderFront:nil];
  151. return NFD_CANCEL;
  152. }
  153. if ( AllocPathSet( urls, outPaths ) == NFD_ERROR )
  154. {
  155. // [pool release];
  156. [keyWindow makeKeyAndOrderFront:nil];
  157. return NFD_ERROR;
  158. }
  159. nfdResult = NFD_OKAY;
  160. }
  161. // [pool release];
  162. [keyWindow makeKeyAndOrderFront:nil];
  163. return nfdResult;
  164. }
  165. nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
  166. const nfdchar_t *defaultPath,
  167. nfdchar_t **outPath ) {
  168. // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  169. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  170. NSSavePanel *dialog = [NSSavePanel savePanel];
  171. [dialog setExtensionHidden:NO];
  172. // Build the filter list.
  173. AddFilterListToDialog(dialog, filterList);
  174. // Set the starting directory
  175. SetDefaultPath(dialog, defaultPath);
  176. nfdresult_t nfdResult = NFD_CANCEL;
  177. if ( [dialog runModal] == NSModalResponseOK )
  178. {
  179. NSURL *url = [dialog URL];
  180. const char *utf8Path = [[url path] UTF8String];
  181. size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  182. *outPath = NFDi_Malloc( byteLen );
  183. if ( !*outPath )
  184. {
  185. // [pool release];
  186. [keyWindow makeKeyAndOrderFront:nil];
  187. return NFD_ERROR;
  188. }
  189. memcpy( *outPath, utf8Path, byteLen );
  190. nfdResult = NFD_OKAY;
  191. }
  192. // [pool release];
  193. [keyWindow makeKeyAndOrderFront:nil];
  194. return nfdResult;
  195. }
  196. nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath,
  197. nfdchar_t **outPath) {
  198. // NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  199. NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow];
  200. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  201. [dialog setAllowsMultipleSelection:NO];
  202. [dialog setCanChooseDirectories:YES];
  203. [dialog setCanCreateDirectories:YES];
  204. [dialog setCanChooseFiles:NO];
  205. // Set the starting directory
  206. SetDefaultPath(dialog, defaultPath);
  207. nfdresult_t nfdResult = NFD_CANCEL;
  208. if ( [dialog runModal] == NSModalResponseOK )
  209. {
  210. NSURL *url = [dialog URL];
  211. const char *utf8Path = [[url path] UTF8String];
  212. // byte count, not char count
  213. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  214. *outPath = NFDi_Malloc( len+1 );
  215. if ( !*outPath )
  216. {
  217. // [pool release];
  218. [keyWindow makeKeyAndOrderFront:nil];
  219. return NFD_ERROR;
  220. }
  221. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  222. nfdResult = NFD_OKAY;
  223. }
  224. // [pool release];
  225. [keyWindow makeKeyAndOrderFront:nil];
  226. return nfdResult;
  227. }