diff --git a/internal/nsalert/alert.m b/internal/nsalert/alert.m index 9933a52..fa9b2fc 100644 --- a/internal/nsalert/alert.m +++ b/internal/nsalert/alert.m @@ -1,30 +1,42 @@ #import #import -void info(char *title, char *message) { +void alert(char *title, char *message, NSAlertStyle style) { NSString* titleString = [NSString stringWithCString:title encoding:[NSString defaultCStringEncoding]]; NSString* messageString = [NSString stringWithCString:message encoding:[NSString defaultCStringEncoding]]; + if([NSThread isMainThread]) { + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = titleString; + alert.informativeText = messageString; + alert.alertStyle = NSAlertStyleInformational; - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = titleString; - alert.informativeText = messageString; - alert.alertStyle = NSAlertStyleInformational; + [alert runModal]; + [alert release]; + return; + } else { + dispatch_group_t wg = dispatch_group_create(); + dispatch_group_enter(wg); + dispatch_async(dispatch_get_main_queue(), ^(void){ + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = titleString; + alert.informativeText = messageString; + alert.alertStyle = NSAlertStyleInformational; - [alert runModal]; - [alert release]; + [alert runModal]; + [alert release]; + dispatch_group_leave(wg); + return; + }); + dispatch_group_wait(wg, DISPATCH_TIME_FOREVER); + } } -void error(char *title, char *message) { - NSString* titleString = [NSString stringWithCString:title encoding:[NSString defaultCStringEncoding]]; - NSString* messageString = [NSString stringWithCString:message encoding:[NSString defaultCStringEncoding]]; - - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = titleString; - alert.informativeText = messageString; - alert.alertStyle = NSAlertStyleCritical; +void info(char *title, char *message) { + alert(title, message, NSAlertStyleInformational); +} - [alert runModal]; - [alert release]; +void error(char *title, char *message) { + alert(title, message, NSAlertStyleCritical); } bool question(char *title, char *message, char *defaultButton, char*alternateButton) { @@ -33,14 +45,36 @@ bool question(char *title, char *message, char *defaultButton, char*alternateBut NSString* defaultButtonString = [NSString stringWithCString:defaultButton encoding:[NSString defaultCStringEncoding]]; NSString* alternateButtonString = [NSString stringWithCString:alternateButton encoding:[NSString defaultCStringEncoding]]; - NSAlert *alert = [[NSAlert alloc] init]; - alert.messageText = titleString; - alert.informativeText = messageString; - [alert addButtonWithTitle:defaultButtonString]; - [alert addButtonWithTitle:alternateButtonString]; + __block NSInteger button; + if([NSThread isMainThread]) { + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = titleString; + alert.informativeText = messageString; + [alert addButtonWithTitle:defaultButtonString]; + [alert addButtonWithTitle:alternateButtonString]; + + button = [alert runModal]; + [alert release]; + } else { + dispatch_group_t wg = dispatch_group_create(); + dispatch_group_enter(wg); + dispatch_async(dispatch_get_main_queue(), ^(void){ + + NSAlert *alert = [[NSAlert alloc] init]; + alert.messageText = titleString; + alert.informativeText = messageString; + [alert addButtonWithTitle:defaultButtonString]; + [alert addButtonWithTitle:alternateButtonString]; + + button = [alert runModal]; + [alert release]; + + dispatch_group_leave(wg); + return; + }); + dispatch_group_wait(wg, DISPATCH_TIME_FOREVER); + } - NSInteger button = [alert runModal]; - [alert release]; if (button == NSAlertFirstButtonReturn) { return true; }