🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.0 KiB

  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. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  101. [dialog setAllowsMultipleSelection:NO];
  102. // Build the filter list
  103. AddFilterListToDialog(dialog, filterList);
  104. // Set the starting directory
  105. SetDefaultPath(dialog, defaultPath);
  106. nfdresult_t nfdResult = NFD_CANCEL;
  107. if ( [dialog runModal] == NSModalResponseOK )
  108. {
  109. NSURL *url = [dialog URL];
  110. const char *utf8Path = [[url path] UTF8String];
  111. // byte count, not char count
  112. size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path);
  113. *outPath = NFDi_Malloc( len+1 );
  114. if ( !*outPath )
  115. {
  116. [pool release];
  117. return NFD_ERROR;
  118. }
  119. memcpy( *outPath, utf8Path, len+1 ); /* copy null term */
  120. nfdResult = NFD_OKAY;
  121. }
  122. [pool release];
  123. return nfdResult;
  124. }
  125. nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList,
  126. const nfdchar_t *defaultPath,
  127. nfdpathset_t *outPaths )
  128. {
  129. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  130. NSOpenPanel *dialog = [NSOpenPanel openPanel];
  131. [dialog setAllowsMultipleSelection:YES];
  132. // Build the fiter list.
  133. AddFilterListToDialog(dialog, filterList);
  134. // Set the starting directory
  135. SetDefaultPath(dialog, defaultPath);
  136. nfdresult_t nfdResult = NFD_CANCEL;
  137. if ( [dialog runModal] == NSModalResponseOK )
  138. {
  139. NSArray *urls = [dialog URLs];
  140. if ( [urls count] == 0 )
  141. {
  142. [pool release];
  143. return NFD_CANCEL;
  144. }
  145. if ( AllocPathSet( urls, outPaths ) == NFD_ERROR )
  146. {
  147. [pool release];
  148. return NFD_ERROR;
  149. }
  150. nfdResult = NFD_OKAY;
  151. }
  152. [pool release];
  153. return nfdResult;
  154. }
  155. nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList,
  156. const nfdchar_t *defaultPath,
  157. nfdchar_t **outPath )
  158. {
  159. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  160. NSSavePanel *dialog = [NSSavePanel savePanel];
  161. [dialog setExtensionHidden:NO];
  162. // Build the filter list.
  163. AddFilterListToDialog(dialog, filterList);
  164. // Set the starting directory
  165. SetDefaultPath(dialog, defaultPath);
  166. nfdresult_t nfdResult = NFD_CANCEL;
  167. if ( [dialog runModal] == NSModalResponseOK )
  168. {
  169. NSURL *url = [dialog URL];
  170. const char *utf8Path = [[url path] UTF8String];
  171. size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1;
  172. *outPath = NFDi_Malloc( byteLen );
  173. if ( !*outPath )
  174. {
  175. [pool release];
  176. return NFD_ERROR;
  177. }
  178. memcpy( *outPath, utf8Path, byteLen );
  179. nfdResult = NFD_OKAY;
  180. }
  181. [pool release];
  182. return nfdResult;
  183. }