// SimpleTable1nn, nibless table demo
// Text-based table only.
// Note that the columns must be identified by identifier strings in IB!
// Variant that doesn't depend on TransSkel.

{$mode objfpc}
{$modeswitch objectivec1}

program SimpleTable1nibless;
uses
	ctypes, MacOSAll, CocoaAll;

// Copied from TransSkel:
function StringToNSString(input: AnsiString): NSString;
begin
	result := NSString.stringWithUTF8String(PChar(input));
	// Autoreleased
end;
function CFStringToString(input: CFStringRef): AnsiString;
var
	output: AnsiString;
	used: Longint;
begin
	CFStringGetBytes( input, CFRangeMake(0, CFStringGetLength(input)),
			kCFStringEncodingUTF8, Ord('^'), false, nil, CFStringGetLength(input)*2, used);
	SetLength(output, used);
	CFStringGetBytes( input, CFRangeMake(0, CFStringGetLength(input)),
			kCFStringEncodingUTF8, Ord('^'), false, @output[1], CFStringGetLength(input)*2, used);
	result := output;
end;
function NSStringToString(input: NSString): AnsiString;
begin
	result := CFStringToString(CFStringRef(input));
end;


var
	table: NSTableView;
	tableData: array of AnsiString;
	tableScores: array of Longint;
	NSApp: NSApplication;
	
type
  DataSource = objcclass(NSObject)
  public
// Data source methods
    function numberOfRowsInTableView(tableView: NSTableView): Longint; message 'numberOfRowsInTableView:';
    function tableView_objectValueForTableColumn_row(tableView: NSTableView; tableColumn: NSTableColumn; row: Longint): id; message 'tableView:objectValueForTableColumn:row:';
// Table view delegate methods
    procedure tableViewSelectionDidChange(notification: NSNotification); message 'tableViewSelectionDidChange:';
	procedure windowWillClose (note: NSNotification); message 'windowWillClose:';
  end;

function DataSource.numberOfRowsInTableView(tableView: NSTableView): Longint;
begin
	WriteLn('numberOfRowsInTableView');
	result := Length(tableData);
end;

function DataSource.tableView_objectValueForTableColumn_row(tableView: NSTableView; tableColumn: NSTableColumn; row: Longint): id;
var
	ident, s: String;
begin
	result := nil;
	if tableColumn.identifier = nil then
		Exit(tableView_objectValueForTableColumn_row);
	ident := NSStringToString(tableColumn.identifier);
	if row <= High(tableData) then
	if row >= Low(tableData) then
	begin
		if ident = '#' then // Row number
		begin
			Str(row, s);
			result := StringToNSString(s);
		end;
		if ident = 'N' then // Name
			result := StringToNSString(tableData[row]);
		if ident = '1' then // Score
		begin
			Str(tableScores[row], s);
			result := StringToNSString(s);
		end;
	end;
end;

// Delegate method
procedure DataSource.tableViewSelectionDidChange(notification: NSNotification);
begin
	WriteLn('Selected row = ', table.selectedRow);
end;

procedure DataSource.windowWillClose (note: NSNotification);
begin
	NSApp.terminate(NSApp);
end;

var
	ds: DataSource;

procedure MakeWindow;
var
	w: NSWindow;
	container: NSScrollView;
	table: NSTableView;
	col1, col2, col3: NSTableColumn;
	frame: NSRect = (origin:(x: 50; y: 60); size:(width: 500; height: 250));
	v: NSView;
begin
	w := NSWindow.alloc;
	w.initWithContentRect_styleMask_backing_defer(frame,
		NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
		NSBackingStoreBuffered, LongBool(0));
	w.setTitle(NSSTR('Nibless table'));
	w.makeKeyAndOrderFront(nil);

	// Set up a scroll view
	container := NSScrollView.alloc.initWithFrame(NSMakeRect(10, 10, 480, 230));
	container.setHasVerticalScroller(true);
	container.setHasHorizontalScroller(true);
	v := w.contentView;
	v.addSubview(container);
	
//	container.setAutoresizesSubviews(true);
	container.setAutoresizingMask(NSViewWidthSizable or NSViewHeightSizable);
	container.release;
	
	// Create the table, put in the scroll view
	table := NSTableView.alloc.initWithFrame(NSMakeRect(12, 11, 386, 153));
	table.setGridStyleMask(NSTableViewSolidVerticalGridLineMask+NSTableViewSolidHorizontalGridLineMask);
	container.setDocumentView(table);

	col1 := NSTableColumn.alloc.initWithIdentifier(NSSTR('#'));
	col1.setWidth(30);
	NSTableHeaderCell(col1.headerCell).setStringValue(NSSTR('Rank'));
	NSCell(col1.dataCell).setEditable(False);
	NSCell(col1.dataCell).setSelectable(True);
	table.addTableColumn(col1);
	
	col2 := NSTableColumn.alloc.initWithIdentifier(NSSTR('N'));
	col2.setWidth(150);
	NSTableHeaderCell(col2.headerCell).setStringValue(NSSTR('Name'));
	NSCell(col2.dataCell).setEditable(False);
	NSCell(col2.dataCell).setSelectable(True);
	table.addTableColumn(col2);
	
	col3 := NSTableColumn.alloc.initWithIdentifier(NSSTR('1'));
	col3.setWidth(75);
	NSTableHeaderCell(col3.headerCell).setStringValue(NSSTR('Score'));
	NSCell(col3.dataCell).setEditable(False);
	NSCell(col3.dataCell).setSelectable(True);
	table.addTableColumn(col3);
	
	SetLength(tableData, 2);
	tableData[0] := 'One item';
	tableData[1] := 'Another item';
	SetLength(tableScores, 2);
	tableScores[0] := 100;
	tableScores[1] := 200;
	
	// Create DataSource
	ds := DataSource.alloc;
	// Connect DataSource to table
	table.setDataSource(ds);
	table.setDelegate(ds);
	w.setDelegate(ds); // For quitting
end;

var
	pool: NSAutoreleasePool;
begin
	pool := NSAutoreleasePool.new;
	NSApp := NSApplication.sharedApplication;
	
	MakeWindow;
	
	// Main loop
	NSApp.run;
	pool.dealloc; // Free;
end.
