// Sheet alert demo, based on CocoaMinimal.

program SheetAlertDemo;
{$mode objfpc}
{$modeswitch objectivec1}

uses
	ctypes, MacOSAll, CocoaAll;

// ------------------ Delegate ---------------------

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

procedure SheetDelegate.sheetDidEnd_returnCode_contextInfo(sheet: NSWindow; returnCode: NSInteger; contextInfo: Pointer);
begin
	WriteLn(returnCode);
	WriteLn(Longint(contextInfo));
end;

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

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

var
	NSApp: NSApplication;
	view: NSView;
	del: SheetDelegate;
	window: NSWindow;

procedure TestView.drawRect (rect: NSRect);
var
		s: NSString;
		attrs: NSMutableDictionary;
		font: NSFont;
begin
		s := NSString(CFSTR('Click for test'));
		font := NSFont.systemFontOfSize(24);
		attrs := NSMutableDictionary.dictionary;
		attrs.setObject_forKey(font, NSFontAttributeName);
		s.drawAtPoint_withAttributes(NSMakePoint(50, 50), attrs);
end;

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

procedure TestView.mouseDown (theEvent: NSEvent);
begin
	NSBeginAlertSheet(
		NSSTR('This is the sheet alert demo'),	// sheet message
		NSSTR('Default'),				// default button label
		NSSTR('Third'),				// third button label
		NSSTR('Other'),				// other button label
		window,							// window sheet is attached to
		del,							// weÕll be our own delegate
		sel_registerName('sheetDidEnd:returnCode:contextInfo:'), // did-end selector
		nil,				   // no need for did-dismiss selector
		Pointer(43),				 // context info - pointer to user data
		NSSTR('with modeless return function in a delegate object.'), []);	// additional text
		// Important: NSBeginAlertSheet ends with "array of const" which requires a [].
end;

// ------------------ Main program ---------------------

var
	pool: NSAutoreleasePool;
	frame: NSRect = (origin:(x: 10; y: 1000); size:(width: 300; height: 130));
	alert: NSAlert;
const
	argsArr: array [0..2] of Integer = (0, 1, 2);
begin
	pool := NSAutoreleasePool.new;
	NSApp := NSApplication.sharedApplication;
	
	window := NSWindow.alloc;
	window.initWithContentRect_styleMask_backing_defer(frame,
		NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
		NSBackingStoreBuffered, LongBool(0));
	window.setTitle(NSString(CFSTR('Sheet alert demo')));
	
	view := TestView.alloc;
	view.initWithFrame(frame);
	window.setContentView(view);
	window.setDelegate(view);
	window.makeKeyAndOrderFront(nil);
	
	// Create sheet delegate!
	del := SheetDelegate.alloc;
	
	// Main loop
	NSApp.run;
	pool.dealloc; // Free;
end.
