#include <stdio.h>
#include <ctype.h>

#include <Cocoa/Cocoa.h>

@interface MyNSWindow : NSWindow
@end

@implementation MyNSWindow
- (BOOL)canBecomeKeyWindow
{
    	return YES;
}

- (BOOL)canBecomeMainWindow
{
	return YES;
}		
@end
		
@interface MyButtonDelegateModal : NSObject
@end

@implementation MyButtonDelegateModal
- (void)buttonPressed
{
	[NSApp stopModal];
}
@end

@interface MyButtonDelegate : NSObject
@end

@implementation MyButtonDelegate
- (void)buttonPressed
{
	NSWindow *win = [[NSWindow alloc] initWithContentRect:NSMakeRect(200, 200, 200, 100) styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];

	id buttonDelegateOK = [[MyButtonDelegateModal alloc] init];
	NSButton *ok = [[NSButton alloc] initWithFrame:NSMakeRect(100, 12, 82, 32)];
	[ok setTitle:@"Close"];
	[ok setButtonType:NSMomentaryPushInButton];
	[ok setBezelStyle:NSRoundedBezelStyle];
	[ok setKeyEquivalent:@"\r"];
	[ok setTarget:buttonDelegateOK];
	[ok setAction:@selector(buttonPressed)];
	[[win contentView] addSubview:ok];
	[ok release];

	[NSApp runModalForWindow:win];

	[win release];
	[buttonDelegateOK release];
}
@end

@interface MyApplication : NSApplication
@end

@implementation MyApplication
@end

int main(int argc, char *argv[])
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
    	[MyApplication sharedApplication];
	
	NSWindow *win = [[MyNSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 640, 480) styleMask:NSTitledWindowMask|NSClosableWindowMask|NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];

	id buttonDelegateOK = [[MyButtonDelegate alloc] init];
	NSButton *ok = [[NSButton alloc] initWithFrame:NSMakeRect(640 - 82 - 14, 12, 82, 32)];
	[ok setTitle:@"Open"];
	[ok setButtonType:NSMomentaryPushInButton];
	[ok setBezelStyle:NSRoundedBezelStyle];
	[ok setKeyEquivalent:@"\r"];
	[ok setTarget:buttonDelegateOK];
	[ok setAction:@selector(buttonPressed)];
	[[win contentView] addSubview:ok];
	[ok release];
		
        [win makeKeyAndOrderFront:nil];

	[NSApp run];
	
	[pool release];
			
	return 0;
}
	