add file dialogs (Cocoa)

Mon, 25 Aug 2025 17:33:52 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 25 Aug 2025 17:33:52 +0200
changeset 724
ba7a2f7675b9
parent 723
e513086eadbb
child 725
f190e440e469

add file dialogs (Cocoa)

ui/cocoa/window.h file | annotate | diff | comparison | revisions
ui/cocoa/window.m file | annotate | diff | comparison | revisions
--- a/ui/cocoa/window.h	Sun Aug 24 15:05:00 2025 +0200
+++ b/ui/cocoa/window.h	Mon Aug 25 17:33:52 2025 +0200
@@ -26,4 +26,5 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#import "toolkit.h"
\ No newline at end of file
+#import "toolkit.h"
+#import "../ui/window.h"
--- a/ui/cocoa/window.m	Sun Aug 24 15:05:00 2025 +0200
+++ b/ui/cocoa/window.m	Mon Aug 25 17:33:52 2025 +0200
@@ -74,3 +74,83 @@
     obj->window = window_data;
     return obj;
 }
+
+
+/* --------------------------------- File Dialogs --------------------------------- */
+
+void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
+    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
+    if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) {
+        openPanel.allowsMultipleSelection = YES;
+    }
+    if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) {
+        openPanel.canChooseFiles = NO;
+        openPanel.canChooseDirectories = YES;
+    }
+    
+    NSWindow *window = (__bridge NSWindow*)obj->wobj;
+    [openPanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse result) {
+        UiEvent event;
+        event.obj = obj;
+        event.window = obj->window;
+        event.document = obj->ctx->document;
+        event.intval = 0;
+        event.set = 0;
+        
+        UiFileList flist = { NULL, 0 };
+        event.eventdata = &flist;
+        event.eventdatatype = UI_EVENT_DATA_FILE_LIST;
+        
+        if(result == NSModalResponseOK) {
+            NSArray<NSURL *> *urls = [openPanel URLs];
+            flist.files = calloc(urls.count, sizeof(char*));
+            for(NSURL *url in urls) {
+                if([url isFileURL]) {
+                    flist.files[flist.nfiles++] = strdup(url.path.UTF8String);
+                }
+            }
+        }
+        
+        if(file_selected_callback) {
+            file_selected_callback(&event, cbdata);
+        }
+        ui_filelist_free(flist);
+    }];
+}
+
+void ui_savefiledialog(UiObject *obj, const char *name, ui_callback file_selected_callback, void *cbdata) {
+    NSSavePanel *savePanel = [NSSavePanel savePanel];
+    if(name) {
+        NSString *nameStr = [[NSString alloc] initWithUTF8String:name];
+        [savePanel setNameFieldStringValue: nameStr];
+    }
+    
+    NSWindow *window = (__bridge NSWindow*)obj->wobj;
+    [savePanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse result) {
+        UiEvent event;
+        event.obj = obj;
+        event.window = obj->window;
+        event.document = obj->ctx->document;
+        event.intval = 0;
+        event.set = 0;
+        
+        UiFileList flist = { NULL, 0 };
+        event.eventdata = &flist;
+        event.eventdatatype = UI_EVENT_DATA_FILE_LIST;
+        
+        if(result == NSModalResponseOK) {
+            NSURL *url = [savePanel URL];
+            if([url isFileURL]) {
+                NSString *path = url.path;
+                flist.files = malloc(sizeof(char*));
+                flist.files[0] = strdup(path.UTF8String);
+                flist.nfiles = 1;
+            }
+            file_selected_callback(NULL, NULL);
+        }
+        if(file_selected_callback) {
+            file_selected_callback(&event, cbdata);
+        }
+        ui_filelist_free(flist);
+    }];
+}

mercurial