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

#include <Cocoa/Cocoa.h>
	
@interface MyButtonDelegate : NSObject
@end

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

@interface MyListDelegate : NSObject
@end

@implementation MyListDelegate
- (int)numberOfRowsInTableView:(NSTableView *)_tableView
{
	return 3;
}

- (id)tableView:(NSTableView *)_tableView objectValueForTableColumn:(NSTableColumn *) tableColumn row:(int)row
{
	return @"Foobar";
}	
@end

@interface MyButtonDelegate2 : NSObject
@end

@implementation MyButtonDelegate2
- (void)buttonPressed
{		
	NSRect rect = NSMakeRect(100, 100, 320 + 2 * 20, 78 + 200 + 20);
	
	NSWindow *win = [[NSWindow alloc] initWithContentRect:rect styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];

	id listDelegate = [[MyListDelegate alloc] init];

	NSScrollView *scrollview = [[NSScrollView alloc] initWithFrame:NSMakeRect(20, 52, rect.size.width - 2 * 20, 200)];
	NSTableView *tableview = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, rect.size.width - 2 * 20 - 16, 200)];
	NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"Column"];
	
	[tableview addTableColumn:column];
	[tableview setDelegate:listDelegate];
	[tableview setDataSource:listDelegate];
	[scrollview setDocumentView:tableview];
	[[win contentView] addSubview:scrollview];
	[scrollview release];
	[tableview release];
	[column release];

	id buttonDelegateOK = [[MyButtonDelegate alloc] init];
	NSButton *ok = [[NSButton alloc] initWithFrame:NSMakeRect(rect.size.width - 82 - 14, 12, 82, 32)];
	[ok setTitle:@"OK"];
	[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 orderOut:nil];
	[win release];
	
	[listDelegate 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 = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 100, 640, 480) styleMask:NSTitledWindowMask|NSClosableWindowMask|NSResizableWindowMask backing:NSBackingStoreBuffered defer:NO];
	
	id buttonDelegateOK = [[MyButtonDelegate2 alloc] init];
	NSButton *ok = [[NSButton alloc] initWithFrame:NSMakeRect(640 - 82 - 14, 12, 82, 32)];
	[ok setTitle:@"OK"];
	[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;
}
	