{$mode objfpc}
{$modeswitch objectivec1}

// Demo of using a sheet for file dialogs, both save and open dialogs.
// This kind of demo is fairly hard to find, but I found one snippet (not complete program)
// at http://www.cocoabuilder.com/archive/cocoa/81757-nssavepanel-example.html

// Note that this is NOT a demo for NSStandardFile but demo for using the underlying NS functions,
// stand-alone NS code.

// Usage: A save dialog is displayed at startup. Click in window for an open dialog.
// No actual file operations are done (no risk for data loss by saving over an existing file).

program FileDialogsTestSheet;
uses
	ctypes, MacOSAll, CocoaAll;

var
	myOpenPanel: NSOpenPanel;
	mySavePanel: NSSavePanel;
	fName: String;
	w1: NSWindow;

type SheetDelegate = objcclass(NSObject)
	public
		procedure sheetDidEnd_returnCode_contextInfo(sheet: NSWindow; returnCode: NSInteger; contextInfo: Pointer);
			message 'sheetDidEnd:returnCode:contextInfo:';
	end;

var
	internalSheetDelegate: SheetDelegate;

procedure SheetDelegate.sheetDidEnd_returnCode_contextInfo(sheet: NSWindow; returnCode: NSInteger; contextInfo: Pointer);
begin
	case Longint(contextInfo) of
		1: WriteLn('Finished save dialog');
		2: WriteLn('Finished open dialog');
	end;
	// Here we can get data from sheet!
end;

// -------------------- View ------------------------

type TestView = objcclass(NSView)
	public
		procedure drawRect (rect: NSRect); override;
		procedure mouseDown (theEvent: NSEvent); override;
		procedure windowWillClose (note: NSNotification); message 'windowWillClose:';
  end;

var
	NSApp: NSApplication;
	view: NSView;

procedure TestView.drawRect (rect: NSRect);
var
	p1: NSPoint = (x: 100; y: 100);
	p2: NSPoint = (x: 200; y: 200);
begin
	NSBezierPath.strokeLineFromPoint_toPoint(p1, p2);
end;

procedure TestView.mouseDown (theEvent: NSEvent);
begin
// Run open file dialog
	myOpenPanel.beginSheetForDirectory_file_types_modalForWindow_modalDelegate_didEndSelector_contextInfo(
		NSSTR('/Documents'),
		NSSTR('the name'),
		nil, // filetypes: NSArray
		w1,
		internalSheetDelegate,
		sel_registerName('sheetDidEnd:returnCode:contextInfo:'), // didEndSaveSheet:returnCode:conextInfo:
		Pointer(2)); // This is where to put user data?
end;

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

function CreateWindow(title: CFStringRef; frame: NSRect): NSWindow;
var
	window: NSWindow;
begin
	window := NSWindow.alloc;
	window.initWithContentRect_styleMask_backing_defer(frame,
		NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
		NSBackingStoreBuffered, LongBool(0));
	window.setTitle(NSString(title));
	
	view := TestView.alloc;
	view.initWithFrame(frame);
	window.setContentView(view);
	window.makeKeyAndOrderFront(nil);
	window.setDelegate(view);
	
	result := window;
end;

var
	pool: NSAutoreleasePool;

	message, defaultButton, alternateButton, otherButton, formatStr: NSString;
	alrt: NSAlert;
	alertResult: Longint;
	frame: NSRect = (origin:(x: 10; y: 1000); size:(width: 400; height: 400));
begin
	// Standard junk
	pool := NSAutoreleasePool.new;
	NSApp := NSApplication.sharedApplication;
	
	w1 := CreateWindow(CFSTR('w1'), frame);
	
	// 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'));
	
	// Create file dialogs. These are constructors, not just copying.
	myOpenPanel := NSOpenPanel.openPanel;
	mySavePanel := NSSavePanel.savePanel;
	
	// Run save file dialog
	internalSheetDelegate := SheetDelegate.alloc;
	mySavePanel.beginSheetForDirectory_file_modalForWindow_modalDelegate_didEndSelector_contextInfo(
		NSSTR('/Documents'),
		NSSTR('the name'),
		w1,
		internalSheetDelegate,
		sel_registerName('sheetDidEnd:returnCode:contextInfo:'), // didEndSaveSheet:returnCode:conextInfo:
		Pointer(1)); // This is where to put user data?

// run the app here
	NSApp.run;
	pool.dealloc;
end.
