Atlas - SDL_cocoadialog.m
Home / ext / SDL / src / dialog / cocoa Lines: 1 | Size: 8578 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2026 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 30#ifdef SDL_VIDEO_DRIVER_COCOA 31extern void Cocoa_SetWindowHasModalDialog(SDL_Window *window, bool has_modal); 32#else 33#define Cocoa_SetWindowHasModalDialog(window, has_modal) 34#endif 35 36static void AddFileExtensionType(NSMutableArray *types, const char *pattern_ptr) 37{ 38 if (!*pattern_ptr) { 39 return; // in case the string had an extra ';' at the end. 40 } 41 42 // -[UTType typeWithFilenameExtension] will return nil if there's a period in the string. It's better to 43 // allow too many files than not allow the one the user actually needs, so just take the part after the '.' 44 const char *dot = SDL_strrchr(pattern_ptr, '.'); 45 NSString *extstr = [NSString stringWithFormat: @"%s", dot ? (dot + 1) : pattern_ptr]; 46 if (@available(macOS 11.0, *)) { 47 UTType *uttype = [UTType typeWithFilenameExtension:extstr]; 48 if (uttype) { // still failed? Don't add the pattern. This is what the pre-macOS11 path does internally anyhow. 49 [types addObject:uttype]; 50 } 51 } else { 52 [types addObject:extstr]; 53 } 54} 55 56static void ReactivateAfterDialog(void) 57{ 58 for (NSRunningApplication *i in [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.apple.dock"]) { 59 [i activateWithOptions:0]; 60 break; 61 } 62 [NSApp activateIgnoringOtherApps:YES]; 63} 64 65void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props) 66{ 67 SDL_Window *window = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL); 68 SDL_DialogFileFilter *filters = SDL_GetPointerProperty(props, SDL_PROP_FILE_DIALOG_FILTERS_POINTER, NULL); 69 int nfilters = (int) SDL_GetNumberProperty(props, SDL_PROP_FILE_DIALOG_NFILTERS_NUMBER, 0); 70 bool allow_many = SDL_GetBooleanProperty(props, SDL_PROP_FILE_DIALOG_MANY_BOOLEAN, false); 71 const char *default_location = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_LOCATION_STRING, NULL); 72 const char *title = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_TITLE_STRING, NULL); 73 const char *accept = SDL_GetStringProperty(props, SDL_PROP_FILE_DIALOG_ACCEPT_STRING, NULL); 74 75 if (filters) { 76 const char *msg = validate_filters(filters, nfilters); 77 78 if (msg) { 79 SDL_SetError("%s", msg); 80 callback(userdata, NULL, -1); 81 return; 82 } 83 } 84 85 if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) { 86 SDL_SetError("File dialog driver unsupported (don't set SDL_HINT_FILE_DIALOG_DRIVER)"); 87 callback(userdata, NULL, -1); 88 return; 89 } 90 91 // NSOpenPanel inherits from NSSavePanel 92 NSSavePanel *dialog; 93 NSOpenPanel *dialog_as_open; 94 95 switch (type) { 96 case SDL_FILEDIALOG_SAVEFILE: 97 dialog = [NSSavePanel savePanel]; 98 break; 99 100 case SDL_FILEDIALOG_OPENFILE: 101 dialog_as_open = [NSOpenPanel openPanel]; 102 [dialog_as_open setAllowsMultipleSelection:((allow_many == true) ? YES : NO)]; 103 dialog = dialog_as_open; 104 break; 105 106 case SDL_FILEDIALOG_OPENFOLDER: 107 dialog_as_open = [NSOpenPanel openPanel]; 108 [dialog_as_open setCanChooseFiles:NO]; 109 [dialog_as_open setCanChooseDirectories:YES]; 110 [dialog_as_open setAllowsMultipleSelection:((allow_many == true) ? YES : NO)]; 111 dialog = dialog_as_open; 112 break; 113 }; 114 115 if (title) { 116 [dialog setTitle:[NSString stringWithUTF8String:title]]; 117 } 118 119 if (accept) { 120 [dialog setPrompt:[NSString stringWithUTF8String:accept]]; 121 } 122 123 if (filters) { 124 // On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString 125 NSMutableArray *types = [[NSMutableArray alloc] initWithCapacity:nfilters]; 126 127 int has_all_files = 0; 128 for (int i = 0; i < nfilters; i++) { 129 char *pattern = SDL_strdup(filters[i].pattern); 130 char *pattern_ptr = pattern; 131 132 if (!pattern_ptr) { 133 callback(userdata, NULL, -1); 134 return; 135 } 136 137 for (char *c = pattern; *c; c++) { 138 if (*c == ';') { 139 *c = '\0'; 140 AddFileExtensionType(types, pattern_ptr); 141 pattern_ptr = c + 1; 142 } else if (*c == '*') { 143 has_all_files = 1; 144 } 145 } 146 147 AddFileExtensionType(types, pattern_ptr); // get the last piece of the string. 148 149 SDL_free(pattern); 150 } 151 152 if (!has_all_files) { 153 if (@available(macOS 11.0, *)) { 154 [dialog setAllowedContentTypes:types]; 155 } else { 156 [dialog setAllowedFileTypes:types]; 157 } 158 } 159 } 160 161 // Keep behavior consistent with other platforms 162 [dialog setAllowsOtherFileTypes:YES]; 163 164 if (default_location && *default_location) { 165 char last = default_location[SDL_strlen(default_location) - 1]; 166 NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:default_location]]; 167 if (last == '/') { 168 [dialog setDirectoryURL:url]; 169 } else { 170 [dialog setDirectoryURL:[url URLByDeletingLastPathComponent]]; 171 [dialog setNameFieldStringValue:[url lastPathComponent]]; 172 } 173 } 174 175 NSWindow *w = NULL; 176 177 if (window) { 178 w = (__bridge NSWindow *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL); 179 if (w) { 180 Cocoa_SetWindowHasModalDialog(window, true); 181 } 182 } 183 184 if (w) { 185 // [dialog beginWithCompletionHandler:^(NSInteger result) { 186 [dialog beginSheetModalForWindow:w completionHandler:^(NSInteger result) { 187 if (result == NSModalResponseOK) { 188 if (dialog_as_open) { 189 NSArray *urls = [dialog_as_open URLs]; 190 const char *files[[urls count] + 1]; 191 for (int i = 0; i < [urls count]; i++) { 192 files[i] = [[[urls objectAtIndex:i] path] UTF8String]; 193 } 194 files[[urls count]] = NULL; 195 callback(userdata, files, -1); 196 } else { 197 const char *files[2] = { [[[dialog URL] path] UTF8String], NULL }; 198 callback(userdata, files, -1); 199 } 200 } else if (result == NSModalResponseCancel) { 201 const char *files[1] = { NULL }; 202 callback(userdata, files, -1); 203 } 204 205 Cocoa_SetWindowHasModalDialog(window, false); 206 ReactivateAfterDialog(); 207 }]; 208 } else { 209 if ([dialog runModal] == NSModalResponseOK) { 210 if (dialog_as_open) { 211 NSArray *urls = [dialog_as_open URLs]; 212 const char *files[[urls count] + 1]; 213 for (int i = 0; i < [urls count]; i++) { 214 files[i] = [[[urls objectAtIndex:i] path] UTF8String]; 215 } 216 files[[urls count]] = NULL; 217 callback(userdata, files, -1); 218 } else { 219 const char *files[2] = { [[[dialog URL] path] UTF8String], NULL }; 220 callback(userdata, files, -1); 221 } 222 } else { 223 const char *files[1] = { NULL }; 224 callback(userdata, files, -1); 225 } 226 227 ReactivateAfterDialog(); 228 } 229} 230 231#endif // SDL_PLATFORM_MACOS 232[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.