diff --git a/delegateuitask_darwin.m b/delegateuitask_darwin.m index a49fda3..023cb49 100644 --- a/delegateuitask_darwin.m +++ b/delegateuitask_darwin.m @@ -9,6 +9,7 @@ // see the hack below; we'll include everything first just in case some other headers get included below; if the hack ever gets resolved, we can use the ones below instead #import #import +#import // HACK. // Apple's header files are bugged: there's an enum that was introduced in 10.7 with new values added in 10.8, but instead of wrapping the whole enum in a version check, they wrap just the fields. This means that on 10.6 that enum will be empty, which is illegal. @@ -33,6 +34,7 @@ #import #import #import +#import extern NSRect dummyRect; @@ -68,6 +70,10 @@ extern NSRect dummyRect; return NSTerminateCancel; } +- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo +{ +} + @end id makeAppDelegate(void) diff --git a/dialog_darwin.go b/dialog_darwin.go index 81819ba..ef1cd6c 100644 --- a/dialog_darwin.go +++ b/dialog_darwin.go @@ -9,10 +9,15 @@ import ( // #include "objc_darwin.h" import "C" -func _msgBox(primarytext string, secondarytext string, style uintptr) { +func _msgBox(parent *Window, primarytext string, secondarytext string, style uintptr) { ret := make(chan struct{}) defer close(ret) uitask <- func() { + var pwin C.id = nil + + if parent != nil { + pwin = parent.sysData.id + } primary := toNSString(primarytext) secondary := C.id(nil) if secondarytext != "" { @@ -20,19 +25,19 @@ func _msgBox(primarytext string, secondarytext string, style uintptr) { } switch style { case 0: // normal - C.msgBox(primary, secondary) + C.msgBox(pwin, primary, secondary) case 1: // error - C.msgBoxError(primary, secondary) + C.msgBoxError(pwin, primary, secondary) } ret <- struct{}{} } <-ret } -func msgBox(primarytext string, secondarytext string) { - _msgBox(primarytext, secondarytext, 0) +func msgBox(parent *Window, primarytext string, secondarytext string) { + _msgBox(parent, primarytext, secondarytext, 0) } -func msgBoxError(primarytext string, secondarytext string) { - _msgBox(primarytext, secondarytext, 1) +func msgBoxError(parent *Window, primarytext string, secondarytext string) { + _msgBox(parent, primarytext, secondarytext, 1) } diff --git a/dialog_darwin.m b/dialog_darwin.m index b147c5a..6bbad61 100644 --- a/dialog_darwin.m +++ b/dialog_darwin.m @@ -3,7 +3,27 @@ #include "objc_darwin.h" #import -static void alert(NSString *primary, NSString *secondary, NSAlertStyle style) +// see delegateuitask_darwin.m +// in this case, NSWindow.h includes NSApplication.h + +#ifdef MAC_OS_X_VERSION_10_7 +#undef MAC_OS_X_VERSION_MIN_REQUIRED +#undef MAC_OS_X_VERSION_MAX_ALLOWED +#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_7 +#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_7 +#endif +#import +#undef MAC_OS_X_VERSION_MIN_REQUIRED +#undef MAC_OS_X_VERSION_MAX_ALLOWED +#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_6 +#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_6 + +#import + +#define to(T, x) ((T *) (x)) +#define toNSWindow(x) to(NSWindow, (x)) + +static void alert(id parent, NSString *primary, NSString *secondary, NSAlertStyle style) { NSAlert *box; @@ -14,15 +34,21 @@ static void alert(NSString *primary, NSString *secondary, NSAlertStyle style) [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]; + if (parent == nil) + [box runModal]; + else + [box beginSheetModalForWindow:toNSWindow(parent) + modalDelegate:[NSApp delegate] + didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) + contextInfo:NULL]; } -void msgBox(id primary, id secondary) +void msgBox(id parent, id primary, id secondary) { - alert((NSString *) primary, (NSString *) secondary, NSInformationalAlertStyle); + alert(parent, (NSString *) primary, (NSString *) secondary, NSInformationalAlertStyle); } -void msgBoxError(id primary, id secondary) +void msgBoxError(id parent, id primary, id secondary) { - alert((NSString *) primary, (NSString *) secondary, NSCriticalAlertStyle); + alert(parent, (NSString *) primary, (NSString *) secondary, NSCriticalAlertStyle); } diff --git a/objc_darwin.h b/objc_darwin.h index e26db6f..fd2058a 100644 --- a/objc_darwin.h +++ b/objc_darwin.h @@ -61,8 +61,8 @@ extern void breakMainLoop(void); extern void cocoaMainLoop(void); /* dialog_darwin.m */ -extern void msgBox(id, id); -extern void msgBoxError(id, id); +extern void msgBox(id, id, id); +extern void msgBoxError(id, id, id); /* listbox_darwin.m */ extern id toListboxItem(id, id);