diff --git a/dialog_darwin.go b/dialog_darwin.go index 6bf7d60..0d1d388 100644 --- a/dialog_darwin.go +++ b/dialog_darwin.go @@ -8,6 +8,7 @@ import ( // #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit // #include "objc_darwin.h" +// #include "dialog_darwin.h" import "C" // NSAlert styles. @@ -27,27 +28,30 @@ var ( _runModal = sel_getUid("runModal") ) -func _msgBox(primarytext string, secondarytext string, style uintptr, button0 string) { +func _msgBox(primarytext string, secondarytext string, style uintptr) { ret := make(chan struct{}) defer close(ret) uitask <- func() { - box := C.objc_msgSend_noargs(_NSAlert, _new) - C.objc_msgSend_id(box, _setMessageText, toNSString(primarytext)) + primary := toNSString(primarytext) + secondary := C.id(nil) if secondarytext != "" { - C.objc_msgSend_id(box, _setInformativeText, toNSString(secondarytext)) + secondary = toNSString(secondarytext) + } + switch style { + case 0: // normal + C.msgBox(primary, secondary) + case 1: // error + C.msgBoxError(primary, secondary) } - C.objc_msgSend_uint(box, _setAlertStyle, C.uintptr_t(style)) - C.objc_msgSend_id(box, _addButtonWithTitle, toNSString(button0)) - C.objc_msgSend_noargs(box, _runModal) ret <- struct{}{} } <-ret } func msgBox(primarytext string, secondarytext string) { - _msgBox(primarytext, secondarytext, _NSInformationalAlertStyle, "OK") + _msgBox(primarytext, secondarytext, 0) } func msgBoxError(primarytext string, secondarytext string) { - _msgBox(primarytext, secondarytext, _NSCriticalAlertStyle, "OK") + _msgBox(primarytext, secondarytext, 1) } diff --git a/dialog_darwin.h b/dialog_darwin.h new file mode 100644 index 0000000..9e113c5 --- /dev/null +++ b/dialog_darwin.h @@ -0,0 +1,4 @@ +/* 15 may 2014 */ + +extern void msgBox(id, id); +extern void msgBoxError(id, id); diff --git a/dialog_darwin.m b/dialog_darwin.m new file mode 100644 index 0000000..73f5a18 --- /dev/null +++ b/dialog_darwin.m @@ -0,0 +1,29 @@ +// 15 may 2014 + +#include "objc_darwin.h" +#include "dialog_darwin.h" +#include + +static void alert(NSString *primary, NSString *secondary, NSAlertStyle style) +{ + NSAlert *box; + + box = [NSAlert new]; + [box setMessageText:primary]; + if (secondary != nil) + [box setInformativeText:secondary]; + [box setAlertStyle:style]; + // TODO is there a named constant? will also need to be changed when we add different dialog types + [box addButtonWithTitle:@"OK"]; + [box runModal]; +} + +void msgBox(id primary, id secondary) +{ + alert((NSString *) primary, (NSString *) secondary, NSInformationalAlertStyle); +} + +void msgBoxError(id primary, id secondary) +{ + alert((NSString *) primary, (NSString *) secondary, NSCriticalAlertStyle); +}