// SimpleTable2, second (slightly less) simple table demo
// This adds icon and controls in tables.
// How can you display images in tables? Set the appropriate column (all of it once) to an NSImageCell!

{$mode objfpc}
{$modeswitch objectivec1}

program SimpleTable;
uses
	ctypes, MacOSAll, CocoaAll, TransSkel5, SkelViewUnit;
var
	table: NSTableView;
	tableData: array of AnsiString;
	tableScores: array of Longint;
	img: NSImage;
	
type
  DataSource = objcclass(NSObject)
  public
// Integer or Longint?
    function numberOfRowsInTableView(tableView: NSTableView): Longint; message 'numberOfRowsInTableView:';
    function tableView_objectValueForTableColumn_row(tableView: NSTableView; tableColumn: NSTableColumn; row: Longint): id; message 'tableView:objectValueForTableColumn:row:';
    procedure tableViewSelectionDidChange(notification: NSNotification); message 'tableViewSelectionDidChange:';
  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
			result := img;
//			Str(tableScores[row], s);
//			result := StringToNSString(s);
		end;
	end;
end;

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

var
	ds: DataSource;

procedure MakeWindow;
var
	w: NSWindow;
	theColumn: NSTableColumn;
begin
	w := SkelGetNewWindow('TableWindow');
	table := NSTableView(SkelGetIndView(w, 'NSTableView', 1));
	
	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);
	
	img := SkelGetPicture('bell1.tga');
	
	// Turn column "1" into an image displaying column!
	theColumn := table.tableColumnWithIdentifier(NSSTR('1'));
	theColumn.setDataCell(NSImageCell.alloc.initImageCell(nil));
	
	// And column "2" is... something else.
	theColumn := table.tableColumnWithIdentifier(NSSTR('2'));
	theColumn.setDataCell(NSStepperCell.alloc.initImageCell(nil));
end;

begin
	SkelInit;					{ initialize }
	SkelApple('About SimpleTable2', nil); // App menu the easy way
	
	MakeWindow;
	
	SkelMain;					{ loop 'til Quit selected }
	SkelClobber;					{ clean up }
end.
