nfd.m 7.5 KB

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