From 428c20d4f58cef7bb1d00895148dbe33bba5dc2d Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 26 Aug 2014 15:26:19 -0400 Subject: [PATCH] Implemented the new dialog system on Mac OS X. --- redo/dialog_darwin.go | 14 ++++++++++---- redo/dialog_darwin.m | 20 +++++++++++--------- redo/objc_darwin.h | 2 +- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/redo/dialog_darwin.go b/redo/dialog_darwin.go index 9e65714..f1882a8 100644 --- a/redo/dialog_darwin.go +++ b/redo/dialog_darwin.go @@ -9,11 +9,17 @@ import ( // #include "objc_darwin.h" import "C" -func openFile() string { - fname := C.openFile() +func (w *window) openFile(f func(filename string)) { + C.openFile(w.id, unsafe.Pointer(&f)) +} + +//export finishOpenFile +func finishOpenFile(fname *C.char, data unsafe.Pointer) { + f := (*func(string))(data) if fname == nil { - return "" + (*f)("") + return } defer C.free(unsafe.Pointer(fname)) - return C.GoString(fname) + (*f)(C.GoString(fname)) } diff --git a/redo/dialog_darwin.m b/redo/dialog_darwin.m index 0557018..4fb9e3e 100644 --- a/redo/dialog_darwin.m +++ b/redo/dialog_darwin.m @@ -3,10 +3,11 @@ #import "objc_darwin.h" #import -char *openFile(void) +#define toNSWindow(x) ((NSWindow *) (x)) + +void openFile(id parent, void *data) { NSOpenPanel *op; - NSInteger ret; op = [NSOpenPanel openPanel]; [op setCanChooseFiles:YES]; @@ -18,11 +19,12 @@ char *openFile(void) [op setExtensionHidden:NO]; [op setAllowsOtherFileTypes:YES]; [op setTreatsFilePackagesAsDirectories:YES]; - beginModal(); - ret = [op runModal]; - endModal(); - if (ret != NSFileHandlingPanelOKButton) - return NULL; - // string freed on the Go side - return strdup([[[op URL] path] UTF8String]); + [op beginSheetModalForWindow:toNSWindow(parent) completionHandler:^(NSInteger ret){ + if (ret != NSFileHandlingPanelOKButton) { + finishOpenFile(NULL, data); + return; + } + // string freed on the Go side + finishOpenFile(strdup([[[op URL] path] UTF8String]), data); + }]; } diff --git a/redo/objc_darwin.h b/redo/objc_darwin.h index d42b806..a8bf282 100644 --- a/redo/objc_darwin.h +++ b/redo/objc_darwin.h @@ -141,6 +141,6 @@ extern void disableAutocorrect(id); extern id toImageListImage(void *, intptr_t, intptr_t, intptr_t); /* dialog_darwin.m */ -extern char *openFile(void); +extern void openFile(id, void *); #endif