diff options
Diffstat (limited to 'Source/SPAlertSheets.m')
-rw-r--r-- | Source/SPAlertSheets.m | 148 |
1 files changed, 84 insertions, 64 deletions
diff --git a/Source/SPAlertSheets.m b/Source/SPAlertSheets.m index fe45741e..f30e6d33 100644 --- a/Source/SPAlertSheets.m +++ b/Source/SPAlertSheets.m @@ -31,54 +31,9 @@ // More info at <http://code.google.com/p/sequel-pro/> #import "SPAlertSheets.h" +#import "SPMainThreadTrampoline.h" -/** - * Provide a simple alias of NSBeginAlertSheet, with a few differences: - * - printf-type format strings are no longer supported within the "msg" - * message text argument, preventing access of random stack areas for - * error text which contains inadvertant printf formatting. - * - The didDismissSelector is no longer supported - * - The sheet no longer needs to be orderOut:ed after use - * - The alert is always shown on the main thread. - */ -void SPBeginAlertSheet( - NSString *title, - NSString *defaultButton, - NSString *alternateButton, - NSString *otherButton, - NSWindow *docWindow, - id modalDelegate, - SEL didEndSelector, - void *contextInfo, - NSString *msg) -{ - NSButton *aButton; - - // Set up an NSAlert with the supplied details - NSAlert *alert = [[[NSAlert alloc] init] autorelease]; - [alert setMessageText:title]; - aButton = [alert addButtonWithTitle:defaultButton]; - [aButton setTag:NSAlertDefaultReturn]; - - // Add 'alternate' and 'other' buttons as appropriate - if (alternateButton) { - aButton = [alert addButtonWithTitle:alternateButton]; - [aButton setTag:NSAlertAlternateReturn]; - } - if (otherButton) { - aButton = [alert addButtonWithTitle:otherButton]; - [aButton setTag:NSAlertOtherReturn]; - } - - // Set the informative message if supplied - if (msg) [alert setInformativeText:msg]; - - // Run the alert on the main thread - [[alert onMainThread] beginSheetModalForWindow:docWindow modalDelegate:modalDelegate didEndSelector:didEndSelector contextInfo:contextInfo]; - - // Ensure the alerting window is frontmost - [[docWindow onMainThread] makeKeyWindow]; -} +@implementation SPAlertSheets /** * Provide a simple alias of a NSApp-wide modal NSBeginAlertSheet which waits @@ -91,21 +46,36 @@ void SPBeginAlertSheet( * - The sheet no longer needs to be orderOut:ed after use * - The alert is always shown on the main thread. */ -void SPBeginWaitingAlertSheet( - NSString *title, - NSString *defaultButton, - NSString *alternateButton, - NSString *otherButton, - NSAlertStyle alertStyle, - NSWindow *docWindow, - id modalDelegate, - SEL didEndSelector, - void *contextInfo, - NSString *msg, - NSString *infoText, - NSInteger *returnCode) ++ (void)beginWaitingAlertSheetWithTitle:(NSString *)title + defaultButton:(NSString *)defaultButton + alternateButton:(NSString *)alternateButton + otherButton:(NSString *)otherButton + alertStyle:(NSAlertStyle)alertStyle + docWindow:(NSWindow *)docWindow + modalDelegate:(id)modalDelegate + didEndSelector:(SEL)didEndSelector + contextInfo:(void *)contextInfo + msg:(NSString *)msg + infoText:(NSString *)infoText + returnCode:(NSInteger *)returnCode { + // Ensure execution on the main thread + if (![[NSThread currentThread] isMainThread]) { + return [[self onMainThread] beginWaitingAlertSheetWithTitle:title + defaultButton:defaultButton + alternateButton:alternateButton + otherButton:otherButton + alertStyle:alertStyle + docWindow:docWindow + modalDelegate:modalDelegate + didEndSelector:didEndSelector + contextInfo:contextInfo + msg:msg + infoText:infoText + returnCode:returnCode]; + } + NSButton *aButton; // Initialize returnCode with a value which can't be returned as @@ -142,10 +112,10 @@ void SPBeginWaitingAlertSheet( if (msg) [alert setMessageText:msg]; // Run the alert on the main thread - [[alert onMainThread] beginSheetModalForWindow:docWindow modalDelegate:modalDelegate didEndSelector:didEndSelector contextInfo:contextInfo]; + [alert beginSheetModalForWindow:docWindow modalDelegate:modalDelegate didEndSelector:didEndSelector contextInfo:contextInfo]; // wait for the sheet - NSModalSession session = [[NSApp onMainThread] beginModalSessionForWindow:[alert window]]; + NSModalSession session = [NSApp beginModalSessionForWindow:[alert window]]; for (;;) { // Since the returnCode can only be -1, 0, or 1 @@ -169,6 +139,56 @@ void SPBeginWaitingAlertSheet( } - [[NSApp onMainThread] endModalSession:session]; - [[NSApp onMainThread] endSheet:[alert window]]; + [NSApp endModalSession:session]; + [NSApp endSheet:[alert window]]; +} + +@end + +/** + * Provide a simple alias of NSBeginAlertSheet, with a few differences: + * - printf-type format strings are no longer supported within the "msg" + * message text argument, preventing access of random stack areas for + * error text which contains inadvertant printf formatting. + * - The didDismissSelector is no longer supported + * - The sheet no longer needs to be orderOut:ed after use + * - The alert is always shown on the main thread. + */ +void SPBeginAlertSheet( + NSString *title, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, + NSWindow *docWindow, + id modalDelegate, + SEL didEndSelector, + void *contextInfo, + NSString *msg) +{ + NSButton *aButton; + + // Set up an NSAlert with the supplied details + NSAlert *alert = [[[NSAlert alloc] init] autorelease]; + [alert setMessageText:title]; + aButton = [alert addButtonWithTitle:defaultButton]; + [aButton setTag:NSAlertDefaultReturn]; + + // Add 'alternate' and 'other' buttons as appropriate + if (alternateButton) { + aButton = [alert addButtonWithTitle:alternateButton]; + [aButton setTag:NSAlertAlternateReturn]; + } + if (otherButton) { + aButton = [alert addButtonWithTitle:otherButton]; + [aButton setTag:NSAlertOtherReturn]; + } + + // Set the informative message if supplied + if (msg) [alert setInformativeText:msg]; + + // Run the alert on the main thread + [[alert onMainThread] beginSheetModalForWindow:docWindow modalDelegate:modalDelegate didEndSelector:didEndSelector contextInfo:contextInfo]; + + // Ensure the alerting window is frontmost + [[docWindow onMainThread] makeKeyWindow]; } |