Fix improper handling of NULL dialog filter on Cocoa

This commit is contained in:
Semphris 2024-05-02 12:22:23 -04:00 committed by Sam Lantinga
parent 9f842e9b5a
commit 4ac8968f65
1 changed files with 47 additions and 43 deletions

View File

@ -37,12 +37,14 @@ void show_file_dialog(cocoa_FileDialogType type, SDL_DialogFileCallback callback
SDL_SetError("tvOS and iOS don't support path-based file dialogs"); SDL_SetError("tvOS and iOS don't support path-based file dialogs");
callback(userdata, NULL, -1); callback(userdata, NULL, -1);
#else #else
const char *msg = validate_filters(filters); if (filters) {
const char *msg = validate_filters(filters);
if (msg) { if (msg) {
SDL_SetError("%s", msg); SDL_SetError("%s", msg);
callback(userdata, NULL, -1); callback(userdata, NULL, -1);
return; return;
}
} }
if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) { if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
@ -73,49 +75,51 @@ void show_file_dialog(cocoa_FileDialogType type, SDL_DialogFileCallback callback
break; break;
}; };
int n = -1; if (filters) {
while (filters[++n].name && filters[n].pattern); int n = -1;
// On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString while (filters[++n].name && filters[n].pattern);
NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:n ]; // On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString
NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:n ];
int has_all_files = 0; int has_all_files = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
char *pattern = SDL_strdup(filters[i].pattern); char *pattern = SDL_strdup(filters[i].pattern);
char *pattern_ptr = pattern; char *pattern_ptr = pattern;
if (!pattern_ptr) { if (!pattern_ptr) {
SDL_OutOfMemory(); SDL_OutOfMemory();
callback(userdata, NULL, -1); callback(userdata, NULL, -1);
return; return;
}
for (char *c = pattern; *c; c++) {
if (*c == ';') {
*c = '\0';
if(@available(macOS 11.0, *)) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
} else {
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
}
pattern_ptr = c + 1;
} else if (*c == '*') {
has_all_files = 1;
} }
}
if(@available(macOS 11.0, *)) { for (char *c = pattern; *c; c++) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]]; if (*c == ';') {
} else { *c = '\0';
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]]; if(@available(macOS 11.0, *)) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
} else {
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
}
pattern_ptr = c + 1;
} else if (*c == '*') {
has_all_files = 1;
}
}
if(@available(macOS 11.0, *)) {
[types addObject: [UTType typeWithFilenameExtension:[NSString stringWithFormat: @"%s", pattern_ptr]]];
} else {
[types addObject: [NSString stringWithFormat: @"%s", pattern_ptr]];
}
SDL_free(pattern);
} }
SDL_free(pattern); if (!has_all_files) {
} if (@available(macOS 11.0, *)) {
[dialog setAllowedContentTypes:types];
if (!has_all_files) { } else {
if (@available(macOS 11.0, *)) { [dialog setAllowedFileTypes:types];
[dialog setAllowedContentTypes:types]; }
} else {
[dialog setAllowedFileTypes:types];
} }
} }