Migrated dialog_darwin.go to use Objective-C directly.

This commit is contained in:
Pietro Gagliardi 2014-05-15 19:08:24 -04:00
parent 93914ecb8c
commit 5b5c76021e
3 changed files with 46 additions and 9 deletions

View File

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

4
dialog_darwin.h Normal file
View File

@ -0,0 +1,4 @@
/* 15 may 2014 */
extern void msgBox(id, id);
extern void msgBoxError(id, id);

29
dialog_darwin.m Normal file
View File

@ -0,0 +1,29 @@
// 15 may 2014
#include "objc_darwin.h"
#include "dialog_darwin.h"
#include <AppKit/NSAlert.h>
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);
}