Atlas - SDL_cocoadialog.m

Home / ext / SDL / src / dialog / cocoa Lines: 1 | Size: 8478 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21#include "SDL_internal.h" 22#include "../SDL_dialog.h" 23#include "../SDL_dialog_utils.h" 24 25#ifdef SDL_PLATFORM_MACOS 26 27#import <Cocoa/Cocoa.h> 28#import <UniformTypeIdentifiers/UTType.h> 29 30extern void Cocoa_SetWindowHasModalDialog(SDL_Window *window, bool has_modal); 31 32static void AddFileExtensionType(NSMutableArray *types, const char *pattern_ptr) 33{ 34 if (!*pattern_ptr) { 35 return; // in case the string had an extra ';' at the end. 36 } 37 38 // -[UTType typeWithFilenameExtension] will return nil if there's a period in the string. It's better to 39 // allow too many files than not allow the one the user actually needs, so just take the part after the '.' 40 const char *dot = SDL_strrchr(pattern_ptr, '.'); 41 NSString *extstr = [NSString stringWithFormat: @"%s", dot ? (dot + 1) : pattern_ptr]; 42 if (@available(macOS 11.0, *)) { 43 UTType *uttype = [UTType typeWithFilenameExtension:extstr]; 44 if (uttype) { // still failed? Don't add the pattern. This is what the pre-macOS11 path does internally anyhow. 45 [types addObject:uttype]; 46 } 47 } else { 48 [types addObject:extstr]; 49 } 50} 51 52static void ReactivateAfterDialog(void) 53{ 54 for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) { 55 [i activateWithOptions:0]; 56 break; 57 } 58 [NSApp activateIgnoringOtherApps:YES]; 59} 60 61void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props) 62{ 63 SDL_Window *window = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL); 64 SDL_DialogFileFilter *filters = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, NULL); 65 int nfilters = (int) SDL_GetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, 0); 66 bool allow_many = SDL_GetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, false); 67 const char *default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL); 68 const char *title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, NULL); 69 const char *accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL); 70 71 if (filters) { 72 const char *msg = validate_filters(filters, nfilters); 73 74 if (msg) { 75 SDL_SetError("%s", msg); 76 callback(userdata, NULL, -1); 77 return; 78 } 79 } 80 81 if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) { 82 SDL_SetError("File dialog driver unsupported (don't set SDL_HINT_FILE_DIALOG_DRIVER)"); 83 callback(userdata, NULL, -1); 84 return; 85 } 86 87 // NSOpenPanel inherits from NSSavePanel 88 NSSavePanel *dialog; 89 NSOpenPanel *dialog_as_open; 90 91 switch (type) { 92 case SDL_FILEDIALOG_SAVEFILE: 93 dialog = [NSSavePanel savePanel]; 94 break; 95 96 case SDL_FILEDIALOG_OPENFILE: 97 dialog_as_open = [NSOpenPanel openPanel]; 98 [dialog_as_open setAllowsMultipleSelection:((allow_many == true) ? YES : NO)]; 99 dialog = dialog_as_open; 100 break; 101 102 case SDL_FILEDIALOG_OPENFOLDER: 103 dialog_as_open = [NSOpenPanel openPanel]; 104 [dialog_as_open setCanChooseFiles:NO]; 105 [dialog_as_open setCanChooseDirectories:YES]; 106 [dialog_as_open setAllowsMultipleSelection:((allow_many == true) ? YES : NO)]; 107 dialog = dialog_as_open; 108 break; 109 }; 110 111 if (title) { 112 [dialog setTitle:[NSString stringWithUTF8String:title]]; 113 } 114 115 if (accept) { 116 [dialog setPrompt:[NSString stringWithUTF8String:accept]]; 117 } 118 119 if (filters) { 120 // On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString 121 NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:nfilters]; 122 123 int has_all_files = 0; 124 for (int i = 0; i < nfilters; i++) { 125 char *pattern = SDL_strdup(filters[i].pattern); 126 char *pattern_ptr = pattern; 127 128 if (!pattern_ptr) { 129 callback(userdata, NULL, -1); 130 return; 131 } 132 133 for (char *c = pattern; *c; c++) { 134 if (*c == ';') { 135 *c = '\0'; 136 AddFileExtensionType(types, pattern_ptr); 137 pattern_ptr = c + 1; 138 } else if (*c == '*') { 139 has_all_files = 1; 140 } 141 } 142 143 AddFileExtensionType(types, pattern_ptr); // get the last piece of the string. 144 145 SDL_free(pattern); 146 } 147 148 if (!has_all_files) { 149 if (@available(macOS 11.0, *)) { 150 [dialog setAllowedContentTypes:types]; 151 } else { 152 [dialog setAllowedFileTypes:types]; 153 } 154 } 155 } 156 157 // Keep behavior consistent with other platforms 158 [dialog setAllowsOtherFileTypes:YES]; 159 160 if (default_location && *default_location) { 161 char last = default_location[SDL_strlen(default_location) - 1]; 162 NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:default_location]]; 163 if (last == '/') { 164 [dialog setDirectoryURL:url]; 165 } else { 166 [dialog setDirectoryURL:[url URLByDeletingLastPathComponent]]; 167 [dialog setNameFieldStringValue:[url lastPathComponent]]; 168 } 169 } 170 171 NSWindow *w = NULL; 172 173 if (window) { 174 w = (__bridge NSWindow *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL); 175 if (w) { 176 Cocoa_SetWindowHasModalDialog(window, true); 177 } 178 } 179 180 if (w) { 181 // [dialog beginWithCompletionHandler:^(NSInteger result) { 182 [dialog beginSheetModalForWindow:w completionHandler:^(NSInteger result) { 183 if (result == NSModalResponseOK) { 184 if (dialog_as_open) { 185 NSArray *urls = [dialog_as_open URLs]; 186 const char *files[[urls count] + 1]; 187 for (int i = 0; i < [urls count]; i++) { 188 files[i] = [[[urls objectAtIndex:i] path] UTF8String]; 189 } 190 files[[urls count]] = NULL; 191 callback(userdata, files, -1); 192 } else { 193 const char *files[2] = { [[[dialog URL] path] UTF8String], NULL }; 194 callback(userdata, files, -1); 195 } 196 } else if (result == NSModalResponseCancel) { 197 const char *files[1] = { NULL }; 198 callback(userdata, files, -1); 199 } 200 201 Cocoa_SetWindowHasModalDialog(window, false); 202 ReactivateAfterDialog(); 203 }]; 204 } else { 205 if ([dialog runModal] == NSModalResponseOK) { 206 if (dialog_as_open) { 207 NSArray *urls = [dialog_as_open URLs]; 208 const char *files[[urls count] + 1]; 209 for (int i = 0; i < [urls count]; i++) { 210 files[i] = [[[urls objectAtIndex:i] path] UTF8String]; 211 } 212 files[[urls count]] = NULL; 213 callback(userdata, files, -1); 214 } else { 215 const char *files[2] = { [[[dialog URL] path] UTF8String], NULL }; 216 callback(userdata, files, -1); 217 } 218 } else { 219 const char *files[1] = { NULL }; 220 callback(userdata, files, -1); 221 } 222 223 ReactivateAfterDialog(); 224 } 225} 226 227#endif // SDL_PLATFORM_MACOS 228
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.