SDL_cocoadialog.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "../SDL_dialog.h"
  20. #include "../SDL_dialog_utils.h"
  21. #ifdef SDL_PLATFORM_MACOS
  22. #import <Cocoa/Cocoa.h>
  23. #import <UniformTypeIdentifiers/UTType.h>
  24. static void AddFileExtensionType(NSMutableArray *types, const char *pattern_ptr)
  25. {
  26. if (!*pattern_ptr) {
  27. return; // in case the string had an extra ';' at the end.
  28. }
  29. // -[UTType typeWithFilenameExtension] will return nil if there's a period in the string. It's better to
  30. // allow too many files than not allow the one the user actually needs, so just take the part after the '.'
  31. const char *dot = SDL_strrchr(pattern_ptr, '.');
  32. NSString *extstr = [NSString stringWithFormat: @"%s", dot ? (dot + 1) : pattern_ptr];
  33. if (@available(macOS 11.0, *)) {
  34. UTType *uttype = [UTType typeWithFilenameExtension:extstr];
  35. if (uttype) { // still failed? Don't add the pattern. This is what the pre-macOS11 path does internally anyhow.
  36. [types addObject:uttype];
  37. }
  38. } else {
  39. [types addObject:extstr];
  40. }
  41. }
  42. void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
  43. {
  44. SDL_Window* window = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL);
  45. SDL_DialogFileFilter *filters = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, NULL);
  46. int nfilters = (int) SDL_GetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, 0);
  47. bool allow_many = SDL_GetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, false);
  48. const char* default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL);
  49. const char* title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, NULL);
  50. const char* accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL);
  51. if (filters) {
  52. const char *msg = validate_filters(filters, nfilters);
  53. if (msg) {
  54. SDL_SetError("%s", msg);
  55. callback(userdata, NULL, -1);
  56. return;
  57. }
  58. }
  59. if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
  60. SDL_SetError("File dialog driver unsupported (don't set SDL_HINT_FILE_DIALOG_DRIVER)");
  61. callback(userdata, NULL, -1);
  62. return;
  63. }
  64. // NSOpenPanel inherits from NSSavePanel
  65. NSSavePanel *dialog;
  66. NSOpenPanel *dialog_as_open;
  67. switch (type) {
  68. case SDL_FILEDIALOG_SAVEFILE:
  69. dialog = [NSSavePanel savePanel];
  70. break;
  71. case SDL_FILEDIALOG_OPENFILE:
  72. dialog_as_open = [NSOpenPanel openPanel];
  73. [dialog_as_open setAllowsMultipleSelection:((allow_many == true) ? YES : NO)];
  74. dialog = dialog_as_open;
  75. break;
  76. case SDL_FILEDIALOG_OPENFOLDER:
  77. dialog_as_open = [NSOpenPanel openPanel];
  78. [dialog_as_open setCanChooseFiles:NO];
  79. [dialog_as_open setCanChooseDirectories:YES];
  80. [dialog_as_open setAllowsMultipleSelection:((allow_many == true) ? YES : NO)];
  81. dialog = dialog_as_open;
  82. break;
  83. };
  84. if (title) {
  85. [dialog setTitle:[NSString stringWithUTF8String:title]];
  86. }
  87. if (accept) {
  88. [dialog setPrompt:[NSString stringWithUTF8String:accept]];
  89. }
  90. if (filters) {
  91. // On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString
  92. NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:nfilters];
  93. int has_all_files = 0;
  94. for (int i = 0; i < nfilters; i++) {
  95. char *pattern = SDL_strdup(filters[i].pattern);
  96. char *pattern_ptr = pattern;
  97. if (!pattern_ptr) {
  98. callback(userdata, NULL, -1);
  99. return;
  100. }
  101. for (char *c = pattern; *c; c++) {
  102. if (*c == ';') {
  103. *c = '\0';
  104. AddFileExtensionType(types, pattern_ptr);
  105. pattern_ptr = c + 1;
  106. } else if (*c == '*') {
  107. has_all_files = 1;
  108. }
  109. }
  110. AddFileExtensionType(types, pattern_ptr); // get the last piece of the string.
  111. SDL_free(pattern);
  112. }
  113. if (!has_all_files) {
  114. if (@available(macOS 11.0, *)) {
  115. [dialog setAllowedContentTypes:types];
  116. } else {
  117. [dialog setAllowedFileTypes:types];
  118. }
  119. }
  120. }
  121. // Keep behavior consistent with other platforms
  122. [dialog setAllowsOtherFileTypes:YES];
  123. if (default_location) {
  124. [dialog setDirectoryURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String:default_location]]];
  125. }
  126. NSWindow *w = NULL;
  127. if (window) {
  128. w = (__bridge NSWindow *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL);
  129. }
  130. if (w) {
  131. // [dialog beginWithCompletionHandler:^(NSInteger result) {
  132. [dialog beginSheetModalForWindow:w completionHandler:^(NSInteger result) {
  133. if (result == NSModalResponseOK) {
  134. if (dialog_as_open) {
  135. NSArray* urls = [dialog_as_open URLs];
  136. const char *files[[urls count] + 1];
  137. for (int i = 0; i < [urls count]; i++) {
  138. files[i] = [[[urls objectAtIndex:i] path] UTF8String];
  139. }
  140. files[[urls count]] = NULL;
  141. callback(userdata, files, -1);
  142. } else {
  143. const char *files[2] = { [[[dialog URL] path] UTF8String], NULL };
  144. callback(userdata, files, -1);
  145. }
  146. } else if (result == NSModalResponseCancel) {
  147. const char *files[1] = { NULL };
  148. callback(userdata, files, -1);
  149. }
  150. }];
  151. } else {
  152. if ([dialog runModal] == NSModalResponseOK) {
  153. if (dialog_as_open) {
  154. NSArray* urls = [dialog_as_open URLs];
  155. const char *files[[urls count] + 1];
  156. for (int i = 0; i < [urls count]; i++) {
  157. files[i] = [[[urls objectAtIndex:i] path] UTF8String];
  158. }
  159. files[[urls count]] = NULL;
  160. callback(userdata, files, -1);
  161. } else {
  162. const char *files[2] = { [[[dialog URL] path] UTF8String], NULL };
  163. callback(userdata, files, -1);
  164. }
  165. } else {
  166. const char *files[1] = { NULL };
  167. callback(userdata, files, -1);
  168. }
  169. }
  170. }
  171. #endif // SDL_PLATFORM_MACOS