// Unusually simple toolbar demo!
// NSToolbar demo by Ingemar Ragnemalm 2010.
// Based on an incomplete and buggy snippet from the "Cocoa in a nutshell" book.
// I fixed the bugs, added a main program, toolbar setup procedure, target
// methods and icons.
// The result is a small and comprehensive demo, much better than any
// other I have seen.

{$mode objfpc}
{$modeswitch objectivec1}
program NutshellToolbar;
uses
	MacOSAll, CocoaAll;

var
	NSApp: NSApplication;

type
	Controller = objcclass(NSObject)
//Required NSToolbar delegate methods
	function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(toolbar:NSToolbar; itemIdentifier: NSString; flag:Boolean): NSToolbarItem; message 'toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:';
	function toolbarDefaultItemIdentifiers(toolbar: NSToolbar): NSArray; message 'toolbarDefaultItemIdentifiers:';
	function toolbarAllowedItemIdentifiers(toolbar: NSToolbar): NSArray; message 'toolbarAllowedItemIdentifiers:';

//actions to handle the various toolbar items and what happens when you click the popups, etc.
	procedure doThis(sender: id); message 'doThis:';
	procedure doThat(sender: id); message 'doThat:';

// Window delegate method for quit
	procedure windowWillClose (note: NSNotification); message 'windowWillClose:';
end;

procedure Controller.doThis(sender: id);
begin
	WriteLn('Do this');
end;
procedure Controller.doThat(sender: id);
begin
	WriteLn('Do that');
end;

function Controller.toolbarAllowedItemIdentifiers(toolbar: NSToolbar): NSArray;
begin 
	result := NSArray.arrayWithObjects(
					NSSTR('Item1'), 
					NSSTR('Item2'), 
					NSToolbarSeparatorItemIdentifier, 
					NSToolbarSpaceItemIdentifier, 
					NSToolbarFlexibleSpaceItemIdentifier, 
					NSToolbarShowColorsItemIdentifier, 
					NSToolbarCustomizeToolbarItemIdentifier, 
					nil);
end;

function Controller.toolbarDefaultItemIdentifiers(toolbar: NSToolbar): NSArray;
begin 
	result := NSArray.arrayWithObjects(
					NSSTR('Item1'), 
					NSToolbarFlexibleSpaceItemIdentifier, 
					NSSTR('Item2'),
					NSToolbarCustomizeToolbarItemIdentifier,
					nil);
end;

function Controller.toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(
	toolbar:NSToolbar; itemIdentifier: NSSTring; flag: Boolean): NSToolbarItem;
var
	item: NSToolbarItem;
begin
	item := NSToolbarItem.alloc.initWithItemIdentifier(itemIdentifier); 
	(* 
	 * If customize the item based on the identifier; 
	 * Standard toolbar items fall through and are returned 
	 *)
	if itemIdentifier.isEqualToString(NSSTR('Item1')) then
	begin
		item.setLabel(NSSTR('Do This'));
		item.setAction(sel_registerName(PChar('doThis:')));
		item.setImage(NSImage.imageNamed(NSSTR('DoThis.png')));
		item.setTarget(self); // Was missing in example code, vital!
// @selector(str) = sel_registerName(PChar('str:'))
	end
	else
	if itemIdentifier.isEqualToString(NSSTR('Item2')) then
	begin
		item.setLabel(NSSTR('Do That'));
		item.setAction(sel_registerName(PChar('doThat:')));
		item.setImage(NSImage.imageNamed(NSSTR('DoThat.png')));
		item.setTarget(self); // Was missing in example code, vital!
	end; 
	result := item.autorelease;
end; 

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

procedure TestToolbar(theWindow: NSWindow);
var
	toolbar: NSToolbar;
	ctrl: Controller;
begin
	toolbar := NSToolbar.alloc.initWithIdentifier(NSSTR('myToolbar'));
	toolbar.autoRelease;
	ctrl := Controller.alloc;
	toolbar.setDelegate(ctrl);

	toolbar.setAllowsUserCustomization(true);
	toolbar.setAutosavesConfiguration(true); 
	toolbar.setDisplayMode(2 {NSToolbarDisplayModeIconOnly});
	
	theWindow.setToolbar(toolbar);
	theWindow.setDelegate(ctrl);
end;

// Standard 
var
	pool: NSAutoreleasePool;
	window: NSWindow;
	frame: NSRect = (origin:(x: 10; y: 1000); size:(width: 400; height: 400));
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(NSSTR('Nutshell Toolbar demo'));
	
	TestToolbar(window);
	window.makeKeyAndOrderFront(nil);
	
	// Main loop
	NSApp.run;
	pool.dealloc; // Free;
end.
