{$mode objfpc}
{$modeswitch objectivec1}

// A nice and small demo on file management and alerts.
// This can serve as base for creating a new file dialog module and simple alerts module.
// Note that this is NOT a demo for NSStandardFile but demo for using the underlying NS functions.

program FileDialogsTest;
uses
	ctypes, MacOSAll, CocoaAll;

var
	myOpenPanel: NSOpenPanel;
	mySavePanel: NSSavePanel;
	fName: String;
	pool: NSAutoreleasePool;

	message, defaultButton, alternateButton, otherButton, formatStr: NSString;
	alrt: NSAlert;
	alertResult: Longint;
	NSApp: NSApplication;
begin
	// Standard junk
	pool := NSAutoreleasePool.new;
	NSApp := NSApplication.sharedApplication;
	
	// Some strings
	message := NSString(CFSTR('This is the message'));
	defaultButton := NSString(CFSTR('Default'));
	alternateButton := NSString(CFSTR('Alt'));
	otherButton := NSString(CFSTR('Other'));
	formatStr := NSString(CFSTR('some extra information'));
	
	// 1. A simple alert
	alrt := NSAlert.alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat(
								message, defaultButton, alternateButton, otherButton, formatStr);
	alertResult := alrt.runModal;
	case alertResult of
		-1: WriteLn('Alert result: Other');
		0: WriteLn('Alert result: Alt');
		1: WriteLn('Alert result: Default');
		otherwise
			WriteLn('Alert result: Unknown');
	end;
	
	// 2. Create file dialogs. These are constructors, not just copying.
	myOpenPanel := NSOpenPanel.openPanel;
	mySavePanel := NSSavePanel.savePanel;
	
	// 3. Run open file dialog
	if myOpenPanel.runModal = NSFileHandlingPanelOKButton then
	begin
		CFStringGetPascalString(CFStringRef(myOpenPanel.filename), @fName, 255, kCFStringEncodingUTF8);
		WriteLn(fName);
		
		// Open and read file as desired
		// using standard calls or NSString.initWithContentsOfFile
	end;
	
	// 4. Run save file dialog
	if mySavePanel.runModal = NSFileHandlingPanelOKButton then
	begin
		CFStringGetPascalString(CFStringRef(mySavePanel.filename), @fName, 255, kCFStringEncodingUTF8);
		WriteLn(fName);

		// Save file as desired
		// using standard calls or NS calls
		// e.g FileContents := NSString.CreateWithHandle/FileContents.writeToFile_atomically etc
	end;

// We can run the app here if we like
//	NSApp.run;
//	pool.Free;
end.
