// 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.

#import <Cocoa/Cocoa.h>

@interface Controller : NSObject { }
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar;
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
- (void) doThis:(id)sender;
- (void) doThat:(id)sender;
@end

@implementation Controller

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar 
{ 
    return [NSArray arrayWithObjects:@"Item1", 
                      @"Item2", 
                      NSToolbarSeparatorItemIdentifier, 
                      NSToolbarSpaceItemIdentifier, 
                      NSToolbarFlexibleSpaceItemIdentifier, 
                      NSToolbarShowColorsItemIdentifier, 
                      NSToolbarCustomizeToolbarItemIdentifier, 
                      nil]; 
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar 
{ 
    return [NSArray arrayWithObjects:@"Item1", 
                      NSToolbarFlexibleSpaceItemIdentifier, 
                      NSToolbarCustomizeToolbarItemIdentifier, 
                      @"Item2", nil]; 
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar 
        itemForItemIdentifier:(NSString *)itemIdentifier 
        willBeInsertedIntoToolbar:(BOOL)flag 
{ 
    NSToolbarItem *item = [[NSToolbarItem alloc] 
                      initWithItemIdentifier:itemIdentifier]; 
    /* 
     * If customize the item based on the identifier; 
     * Standard toolbar items fall through and are returned 
     */ 
    if ( [itemIdentifier isEqualToString:@"Item1"] )
    {
        [item setLabel:@"Do This"]; 
        [item setAction:@selector(doThis:)]; 
        [item setImage:[NSImage imageNamed:@"DoThis"]]; 
		[item setTarget:self]; // Was missing in example code, vital!
    } else if ( [itemIdentifier isEqualToString:@"Item2"])
    {
        [item setLabel:@"Do That"]; 
        [item setAction:@selector(doThat:)]; 
        [item setImage:[NSImage imageNamed:@"DoThat"]]; 
		[item setTarget:self]; // Was missing in example code, vital!
    } 
    return [item autorelease]; 
} 

- (void) doThis:(id)sender
{
	printf("Do this\n");
}

- (void) doThat:(id)sender
{
	printf("Do that\n");
}

-(void)windowWillClose:(NSNotification *)note
{
    [[NSApplication sharedApplication] terminate:self];
}

@end

void TestToolbar(NSWindow *theWindow)
{
	NSToolbar *toolbar;
	Controller *ctrl;
	
	toolbar = [[NSToolbar alloc]initWithIdentifier:@"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]; // For quit
}

NSApplication *myApp;
NSAutoreleasePool *pool;
NSWindow *window;

int main(int argc, const char *argv[])
{
	pool = [NSAutoreleasePool new];
	myApp = [NSApplication sharedApplication];
	NSRect frame = NSMakeRect(10., 1000., 400., 400.);
	
	window = [NSWindow alloc];
	[window initWithContentRect:frame
                  styleMask:NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask
                    backing:NSBackingStoreBuffered
                      defer:false];
    [window setTitle:@"Nutshell Toolbar demo"];
	
	TestToolbar(window);
	[window makeKeyAndOrderFront:nil];

	// Main loop
	[myApp run];
	[pool release]; // Free;
	return( EXIT_SUCCESS );
}
