[TxMt] DIALOG2 - alert: display as sheet > suggestion

Hans-Jörg Bibiko bibiko at eva.mpg.de
Tue Mar 25 18:47:00 UTC 2008


HI,

sorry for maybe double-posting it, but I got an error for textmate- 
dev list. (?)
__


I've just had a look at the new DIALOG2 stuff. Very nice ;)

Here comes a suggestion to allow the user to display the alert as
sheet bound to the current document window:

If option -e or 'sheet' is given it shows the alert as sheet.

CODE:
######################################################################## 
##

#import "../Dialog2.h"
#import "../TMDCommand.h"
#import "../OptionParser.h"

/*
echo '{alertStyle = warning; buttonTitles = ('OK'); messageTitle =
'test'; informativeText = 'Testing';}' | "$DIALOG" alert

"$DIALOG" help alert
"$DIALOG" alert -s critical -m "FOOL!" -t "test" -1 foo -2 bar -3 baz
"$DIALOG" alert -e -s critical -m "FOOL!" -t "test" -1 foo -2 bar -3 baz
*/

// =========
// = Alert =
// =========

@interface TMDAlertCommand : TMDCommand
{
}
@end

@implementation TMDAlertCommand

int sheetReturn;

+ (void)load
{
	[super registerObject:[self new] forCommand:@"alert"];
}

static option_t const expectedOptions[] =
{
	{ "m", "message",			option_t::required_argument,	
option_t::string,		"Message title."},
	{ "t", "text",				option_t::required_argument,	
option_t::string,		"Informative text for the alert."},
	{ "s", "alert-style",	option_t::required_argument,	
option_t::string,		"One of warning, critical or informational (the
default)."},
	{ "1", "button1",			option_t::required_argument,	
option_t::string,		"Button 1 label."},
	{ "2", "button2",			option_t::required_argument,	
option_t::string,		"Button 2 label."},
	{ "3", "button3",			option_t::required_argument,	
option_t::string,		"Button 3 label."},
	{ "e", "sheet",			option_t::no_argument, option_t::none,		"If given
displays alert as sheet."},
};

- (void)handleCommand:(CLIProxy*)proxy
{
	// NSFileHandle* fh = [options objectForKey:@"stderr"];

	SetOptionTemplate(proxy, expectedOptions);
	NSAlertStyle	alertStyle        = NSInformationalAlertStyle;
	NSString			*alertStyleString = [proxy valueForOption:@"alert-style"];
	NSDictionary	*resultDict       = nil;
		
	NSAlert *alert = [[[NSAlert alloc] init] autorelease];
	int offset = 0;
	if([alertStyleString isEqualToString:@"warning"])
	{
		alertStyle = NSWarningAlertStyle;
	}
	else if([alertStyleString isEqualToString:@"critical"])
	{
		alertStyle = NSCriticalAlertStyle;
	}
	else if([alertStyleString isEqualToString:@"informational"])
	{
		alertStyle = NSInformationalAlertStyle;
	}
	
	[alert setAlertStyle:alertStyle];
	if([proxy valueForOption:@"message"])
		[alert setMessageText:[proxy valueForOption:@"message"]];
	if([proxy valueForOption:@"text"])
		[alert setInformativeText:[proxy valueForOption:@"text"]];
	
	// Setup buttons
	offset = NSAlertFirstButtonReturn;
	if ([proxy valueForOption:@"button1"])
		[alert addButtonWithTitle:[proxy valueForOption:@"button1"]];
	if ([proxy valueForOption:@"button2"])
		[alert addButtonWithTitle:[proxy valueForOption:@"button2"]];
	if ([proxy valueForOption:@"button3"])
		[alert addButtonWithTitle:[proxy valueForOption:@"button3"]];
	
	BOOL modal = YES;
	if([proxy valueForOption:@"sheet"])
		modal = NO;
	
	// Show the alert
	
	if(modal)
	{
		int alertResult = ([alert runModal] - NSAlertFirstButtonReturn);
		
		resultDict = [NSDictionary dictionaryWithObject:[NSNumber
numberWithInt:alertResult] forKey:@"buttonClicked"];
		[TMDCommand writePropertyList:[NSString
stringWithFormat:@"%d",sheetReturn - offset] toFileHandle:[proxy
outputHandle]];
	}
	else
	{
		id documentWindow =nil;
		enumerate([NSApp windows], id item)
		if([item isKindOfClass:NSClassFromString(@"NSWindow")])
			if([item isKeyWindow])
				documentWindow = item;
		
		sheetReturn = -1;
		
		[alert beginSheetModalForWindow:documentWindow
						  modalDelegate:self
						 didEndSelector:@selector(mySheetDidEnd:returnCode:contextInfo:)
							contextInfo:NULL];
		[NSApp runModalForWindow:[alert window]];
		[TMDCommand writePropertyList:[NSString
stringWithFormat:@"%d",sheetReturn - offset] toFileHandle:[proxy
outputHandle]];
	}
}

-(id)mySheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode
contextInfo:(void *)contextInfo
{
	[NSApp endSheet:sheet];
	sheetReturn = returnCode;
	[NSApp stopModal];
	return nil;
}

- (NSString *)commandDescription
{
	return @"Show an alert box.";
}

- (NSString *)usageForInvocation:(NSString *)invocation;
{
	return [NSString stringWithFormat:@"%@ «options»\n\nOptions:\n%@",
invocation, GetOptionList(expectedOptions)];
}
@end

######################################################################## 
##

Any comments?


Cheers,

--Hans


More information about the textmate mailing list