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" // #include "objc_darwin.h"
import "C" import "C"
func openFile() string { func (w *window) openFile(f func(filename string)) {
fname := C.openFile() C.openFile(w.id, unsafe.Pointer(&f))
}
//export finishOpenFile
func finishOpenFile(fname *C.char, data unsafe.Pointer) {
f := (*func(string))(data)
if fname == nil { if fname == nil {
return "" (*f)("")
return
} }
defer C.free(unsafe.Pointer(fname)) 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 "objc_darwin.h"
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
char *openFile(void) #define toNSWindow(x) ((NSWindow *) (x))
void openFile(id parent, void *data)
{ {
NSOpenPanel *op; NSOpenPanel *op;
NSInteger ret;
op = [NSOpenPanel openPanel]; op = [NSOpenPanel openPanel];
[op setCanChooseFiles:YES]; [op setCanChooseFiles:YES];
@ -18,11 +19,12 @@ char *openFile(void)
[op setExtensionHidden:NO]; [op setExtensionHidden:NO];
[op setAllowsOtherFileTypes:YES]; [op setAllowsOtherFileTypes:YES];
[op setTreatsFilePackagesAsDirectories:YES]; [op setTreatsFilePackagesAsDirectories:YES];
beginModal(); [op beginSheetModalForWindow:toNSWindow(parent) completionHandler:^(NSInteger ret){
ret = [op runModal]; if (ret != NSFileHandlingPanelOKButton) {
endModal(); finishOpenFile(NULL, data);
if (ret != NSFileHandlingPanelOKButton) return;
return NULL; }
// string freed on the Go side // string freed on the Go side
return strdup([[[op URL] path] UTF8String]); 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); extern id toImageListImage(void *, intptr_t, intptr_t, intptr_t);
/* dialog_darwin.m */ /* dialog_darwin.m */
extern char *openFile(void); extern void openFile(id, void *);
#endif #endif