Implemented the new dialog system on Mac OS X.

This commit is contained in:
Pietro Gagliardi 2014-08-26 15:26:19 -04:00
parent ff2ad05270
commit 428c20d4f5
3 changed files with 22 additions and 14 deletions

View File

@ -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))
}

View File

@ -3,10 +3,11 @@
#import "objc_darwin.h"
#import <Cocoa/Cocoa.h>
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);
}];
}

View File

@ -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